Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPA OneToMany <-> ManyToOne Relation will not stored in DB

843830Jun 10 2009 — edited Mar 22 2010
Hi,

I have the following problem:

I have two tables:
Logon
-username
-password
-company_id

Company
-Name

So Company has a OneToMany Relation to Logon and Logon a ManyToOne to company

I have created the following Entity classes:
class Logon{
...
    @ManyToOne()
    @JoinColumn(name = "COMPANY_ID",insertable=false, updatable=false)
    private Company company;
...
}

class Company{
...
    @OneToMany(mappedBy="company", cascade=CascadeType.ALL)
    private Collection<Logon> logons = new Vector<Logon>();
}
And here is the code to insert the data.
        Logon logon = new Logon();
        Company company = new Company();
        logon.setCompany(company);
        company.addLogon(logon);

        EntityTransaction trans = em.getTransaction();
        trans.begin()
        em.persist(logon);
        em.persist(company);
        trans.commit();
After that code I have an entry in table Logon and an entry in table Company.
But the field company_id in table logon is always set to 0 and not to the company's id.

The tables where not created from the entities. I have to work with an existing MySQL DB.

Does someone has an idea?

regards
Gerald
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2010
Added on Jun 10 2009
11 comments
1,293 views