Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » FK value setting in a ManyToOne relationship
FK value setting in a ManyToOne relationship [message #646656] Wed, 29 December 2010 11:51 Go to next message
No real name is currently offline No real name
Messages: 1
Registered: December 2010
Junior Member
Hi, I have a OneToMany relationship between entities Contact and Email. When I create a Contact and add to him 1 or more Email I need to set the FK column in Email table to the right Contact of that email...

I tried this:
@Entity
class Contact{
     @Id
     ......
    @OneToMany(mappedBy="contact", cascade=CascadeType.ALL)
    private Set<ContactEmail> email;
}

@Entity
class Email{
     @Id
     ......
    @ManyToOne
    @JoinColumn(name="CONTACT_ID")
    private Contact contact;
}



When in my test package I call:

em.persist(contact); //(where contact has 2 emails associated)

it creates 1 contact, 2 emails records but the FK column "CONTACT_ID" is empty...

how can I set its value with the right Contact ID of that email?

thanks in advance!

[Updated on: Wed, 29 December 2010 11:53]

Report message to a moderator

Re: FK value setting in a ManyToOne relationship [message #647088 is a reply to message #646656] Tue, 04 January 2011 09:54 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1018
Registered: July 2009
Senior Member
Hello,

Your object model implies that each Email in the list of Contact's email must have a Contact. Are you setting the Email->Contact relationship? This relationship is what controls the foreign key in the Email table, not the Contact->Email relationship. JPA requires that applications set both sides of a relationship if it is bidirectional so that the object model is in synch with the database.

Best Regards,
Chris
Previous Topic:Using OneToMany or ManyToOne . A Performance issue
Next Topic:persist() kicking up error about unknown entity type
Goto Forum:
  


Current Time: Sat Aug 30 08:39:02 EDT 2014

Powered by FUDForum. Page generated in 0.08189 seconds