Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Mapping/Relationship design question

Roger,
   Hi, good question.
   1)    mappedBy
This is not defaulted - so without this attribute you will default to unidirectional relationships with a JoinTable. ManyToOne relationships are always the owner - so the mappedBy goes on the inverse OneToMany or OneToOne Since the Phone --> Person relationship is the owner - then the mappedBy attribute goes on the inverse (the Person --> Phone - the 1:M).
      This annotation attribute goes on the inverse side.
   2)   JoinColumn
This is defaulted to the (source-relationship_name + "_" + target-pk-name)
      You can override this if your db column name already exists.
      This annotation goes on the owner side.
   3)   @PrimaryKeyJoinColumn
You may want to check out usage of this annotation to enforce constraints using shared PK's - I have not worked this this annotation yet - so I can't comment at this time.


You bidirectional relationship can be modeled using an owning relationship (OneToOne (with PK constraint) or ManyToOne) from MobilePhoneNumber to Person, and an inverse OneToMany relationship from Person to MobilePhoneNumber. The only difference between a @OneToOne and a @ManyToOne is the fact the the source FK has a uniqueness constraint for the @OneToOne on the database (internally EclipseLink treats both mappings as OneToOne). An implicit @JoinColumn will be created for the owning @*ToOne (if not specified)

   An example:
-------------------
I tested both versions of MobilePhoneNumber M:1 and 1:1 by using our own jpa/metamodel test classes Manufacturer 1 <--> * Computer.

@Entity
public class Person implements java.io.Serializable{
... // JoinColumn is on the owner - then we need a mappedBy on the inverse side here @OneToMany(mappedBy="person") private Collection<MobilePhoneNumber> phones;
   // or using a specific Collection class
//private Set<MobilePhoneNumber> phones = new HashSet<MobilePhoneNumber>();
}

@Entity
public class MobilePhoneNumber implements java.io.Serializable {
...
// The M:1 side is the owning side for "phones" - JoinColumn may be specified here
   //@ManyToOne // No uniqueness
   @OneToOne // With FK uniqueness constraint
   private Person person;
}

   hope this is a start
   /michael

RogerV wrote:
Hi

I'm having a problem mapping Person entities to MobilePhoneNumber entities.
For my purposes, a Person can have many mobile phones (e.g. work, personal)
(@OneToMany) but each MobilePhoneNumber belongs to a single Person
(@OneToOne). How do I define the annotations to get this to work and what do
I use "mappedBy" or "JoinColumns" - or have I got the relationships wrong.

Regards



Back to the top