Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Editing Derived Ids in JPA Diagram Editor"

m
m (The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses IdClass to represent a composite key.)
 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Overview  ==
 
== Overview  ==
  
Every entity must have a primary key.
+
Each entity must have a primary key. Primary key must be defined exactly once in an entity hierarchy by one of the following approaches:
The primary key must be defined on the entity class that is the root of the entity hierarchy or on a mapped superclass that is a (direct or indirect) superclass of all entity classes in the entity hierarchy. The primary key must be defined exactly once in an entity hierarchy.
+
* on the entity class himself;
 +
* on the entity class that is the root of the entity hierarchy;
 +
* on a mapped superclass that is a (direct or indirect) superclass of all entity classes in the entity hierarchy.  
  
 
== Derived Ids ==
 
== Derived Ids ==
  
The identity of an entity may be derived from the identity of another entity (the "parent" entity) when the former entity (the "dependent" entity) is the owner of a many-to-one or one-to-one relationship to the parent entity and a foreign key maps the relationship from dependent to parent.
+
When there is an uni- or bi-directional relationship of type '''''one-to-one''''' and '''''many-to-one''''' the owner of the relationship can derive identity from the target entity.
  
In palette of the JPA diagram editor, there shall be a new section, named '''''Derived Identifiers''''', which shall contain two palette entry: '''''One-to-One''''' and '''''Many-to-One''''':
+
In palette of the JPA diagram editor, there shall be a new section, named '''''Derived Identifiers''''', which shall contain the following palette entries: '''''One-to-One''''' uni- and bi-directional and '''''Many-to-One''''' uni- and bi-directional relationships:
  
 
[[Image: DerivedIdentifiersPalette.jpg]]
 
[[Image: DerivedIdentifiersPalette.jpg]]
Line 31: Line 33:
 
   @Id
 
   @Id
 
   @OneToOne
 
   @OneToOne
   @JoinColumn(name="FK")
+
   @JoinColumn(name="Person_ssn")
 
   Person patient;
 
   Person patient;
 
}
 
}
Line 38: Line 40:
 
In the JPA editor it means that there shall be an entity ''Person'' with primary key ''ssn'' and an entity ''MedicalHistory'' without any primary keys. When the '''One-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity and shall be mapped as primary key:
 
In the JPA editor it means that there shall be an entity ''Person'' with primary key ''ssn'' and an entity ''MedicalHistory'' without any primary keys. When the '''One-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity and shall be mapped as primary key:
  
[[Image: OneToOneSinglePK.jpg]]
+
[[Image: OneToOneSinglePK1.jpg]]
  
==== The parent entity has a simple primary key and the dependent entity has a single primary key attribute corresponding to the relationship attribute. The primary key attribute is of the same basic type as the primary key of the parent entity. ====
+
==== The parent entity has a simple primary key and the dependent entity has a single primary key attribute corresponding to the relationship attribute. ====
  
 
The source code shall look like this:
 
The source code shall look like this:
Line 55: Line 57:
 
   // default join column name is overridden
 
   // default join column name is overridden
 
   @MapsId
 
   @MapsId
   @JoinColumn(name="FK")
+
   @JoinColumn(name="Person_ssn")
 
   @OneToOne Person patient;
 
   @OneToOne Person patient;
 
}
 
}
Line 62: Line 64:
 
In the JPA editor it means that there shall be an entity ''Person'' with primary key ''ssn'' and an entity ''MedicalHistory'' with primary key ''id''. When the '''One-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity and shall be mapped as primary key:
 
In the JPA editor it means that there shall be an entity ''Person'' with primary key ''ssn'' and an entity ''MedicalHistory'' with primary key ''id''. When the '''One-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity and shall be mapped as primary key:
  
[[Image: OneToOneSharedPK.jpg‎]]
+
[[Image: OneToOneSharedPK1.jpg‎]]
  
 
==== The parent entity has a simple primary key and the dependent entity uses IdClass to represent a composite key. ====
 
==== The parent entity has a simple primary key and the dependent entity uses IdClass to represent a composite key. ====
Line 78: Line 80:
 
public class DependentId {
 
public class DependentId {
 
   String name; // matches name of @Id attribute
 
   String name; // matches name of @Id attribute
   long emp; // matches name of @Id attribute and type of Employee PK
+
   long employee; // matches name of @Id attribute and type of Employee PK
 
}
 
}
  
Line 88: Line 90:
 
   @Id
 
   @Id
 
   @ManyToOne
 
   @ManyToOne
   Employee emp;
+
   Employee employee;
 
}
 
}
 
</source>
 
</source>
  
In the JPA editor it means that there shall be an entity ''Employee'' with primary key ''id'' and an entity ''Dependent'' with composite primary key ''name''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the primary key of the ''Employee'' entity. If such an attribute is missing it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the ''Dependent'' entity and shall be mapped as primary key:
+
In the JPA editor it means that there shall be an entity ''Employee'' with primary key ''id'' and an entity ''Dependent'' with composite primary key ''name''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the primary key of the ''Employee'' entity. If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the ''Dependent'' entity and shall be mapped as primary key:
  
[[Image: ManyToOneDepIdClass.jpg]]
+
[[Image: ManyToOneDepIdClass1.jpg]]
  
 
==== The parent entity has a simple primary key and the dependent entity uses EmbeddedId to represent a composite key. ====
 
==== The parent entity has a simple primary key and the dependent entity uses EmbeddedId to represent a composite key. ====
Line 105: Line 107:
 
   @Id
 
   @Id
 
   long id;
 
   long id;
  String empName;
 
 
}
 
}
  
Line 120: Line 121:
 
   @MapsId("empPK") // maps empPK attribute of embedded id
 
   @MapsId("empPK") // maps empPK attribute of embedded id
 
   @ManyToOne
 
   @ManyToOne
   Employee emp;
+
   Employee employee;
 
}
 
}
 
</source>
 
</source>
  
In the JPA editor it means that there shall be an entity ''Employee'' with primary key ''id'', an embeddable ''DependentId'' and an entity ''Dependent'' that uses the emebeddable as '''EmbeddedId'''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the primary key of the ''Employee'' entity. If such an attribute is missing it shall be added authomatically. When the new connection is created, a new attribute of of the parent entity shall be added in the ''Dependent'' entity and shall be mapped as id by the parent entity's attribute in the embeddable class:
+
In the JPA editor it means that there shall be an entity ''Employee'' with primary key ''id'', an embeddable ''DependentId'' and an entity ''Dependent'' that uses the emebeddable as '''EmbeddedId'''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the primary key of the ''Employee'' entity. If such an attribute does not exists, it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the ''Dependent'' entity and shall be mapped as id by the parent entity's attribute name in the embeddable class:
  
[[Image: ManyToOneDepEmbeddedId.jpg]]
+
[[Image: ManyToOneDepEmbeddedId1.jpg]]
 +
 
 +
==== The parent entity has a composite primary key (IdClass) and the dependent entity uses the same class to represent a composite key (IdClass). ====
 +
 
 +
The source code shall look like this:
 +
 
 +
<source lang="java">
 +
public class PersonId {
 +
  String firstName;
 +
  String lastName;
 +
}
 +
 +
@Entity
 +
@IdClass(PersonId.class)
 +
public class Person {
 +
  @Id String firstName;
 +
  @Id String lastName;
 +
}
 +
 +
@Entity
 +
@IdClass(PersonId.class)
 +
public class MedicalHistory {
 +
  @Id
 +
  @JoinColumns({
 +
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
 +
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
 +
  })
 +
  @OneToOne
 +
  Person patient;
 +
}
 +
</source>
 +
 
 +
In the JPA editor it means that there shall be an entity ''Person'' with composite primary key (IdClass) that has two fields ''firstName'' and ''lastName'' and an entity ''MedicalHistory'' that has composite key using the same IdClass. When the '''One-to-One''' uni-directional relation feature is selected from the '''Derived Identifiers''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity, all of the primary key attributes of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
 +
 
 +
[[Image: OneToOneSameIdClass1.jpg ]]
 +
 
 +
==== The parent entity has a composite primary key (IdClass) and the dependent entity uses the same class to represent a composite key (EmbeddedId). ====
 +
 
 +
The source code shall look like this:
 +
 
 +
<source lang="java">
 +
@Embeddable
 +
public class PersonId {
 +
  String firstName;
 +
  String lastName;
 +
}
 +
 +
@Entity
 +
@IdClass(PersonId.class)
 +
public class Person {
 +
  @Id String firstName;
 +
  @Id String lastName;
 +
}
 +
 
 +
@Entity
 +
public class MedicalHistory {
 +
  //all attributes map to relationship: AttributeOverride not allowed
 +
  @EmbeddedId
 +
  PersonId id;
 +
  @MapsId
 +
  @JoinColumns({
 +
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
 +
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
 +
  })
 +
  @OneToOne
 +
  Person patient;
 +
}
 +
</source>
 +
 
 +
In the JPA editor it means that there shall be an entity Person with composite primary key (IdClass) that has two fields firstName and lastName, an embeddable (the same IdClass) and an entity ''MedicalHistory'' that uses the emebeddable as '''EmbeddedId'''. When the '''One-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity and all of the primary key attributes of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
 +
 
 +
[[Image: OneToOneSameIdClassEmbeddedId.jpg ]]
 +
 
 +
==== The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses the same class to represent a composite key (IdClass). ====
 +
 
 +
The source code shall look like this:
 +
 
 +
<source lang="java">
 +
@Embeddable
 +
public class PersonId {
 +
  String firstName;
 +
  String lastName;
 +
}
 +
 +
@Entity
 +
public class Person {
 +
  @EmbeddedId
 +
  PersonId id;
 +
}
 +
 +
@Entity
 +
@IdClass(PersonId.class)
 +
public class MedicalHistory {
 +
  @Id
 +
  @JoinColumns({
 +
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
 +
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
 +
  })
 +
  @OneToOne
 +
  Person patient;
 +
}
 +
</source>
 +
 
 +
In the JPA editor it means that there shall be an embeddable ''PersonId'' with two attributes ''firstName'' and ''lastName'', an entity ''Person'' that useses the embeddable as '''EmbeddedId''' and an entity ''MedicalHistory'' that has composite key using the same embeddable as IdClass. When the '''One-to-One''' uni-directional relation feature is selected from the '''Derived Identifiers''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity, all of the attributes of the embeddable class shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
 +
 
 +
[[Image: OneToOneSameEmbeddedIdIdClass.jpg ]]
 +
 
 +
==== The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses the same class to represent a composite key (EmbeddedId). ====
 +
 
 +
The source code shall look like this:
 +
 
 +
<source lang="java">
 +
@Embeddable
 +
public class PersonId {
 +
  String firstName;
 +
  String lastName;
 +
}
 +
 +
@Entity
 +
public class Person {
 +
  @EmbeddedId
 +
  PersonId id;
 +
}
 +
 
 +
@Entity
 +
public class MedicalHistory {
 +
  //all attributes map to relationship: AttributeOverride not allowed
 +
  @EmbeddedId
 +
  PersonId id;
 +
  @MapsId
 +
  @JoinColumns({
 +
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
 +
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
 +
  })
 +
  @OneToOne
 +
  Person patient;
 +
}
 +
</source>
 +
 
 +
In the JPA editor it means that there shall be an an embeddable ''PersonId'' with two attributes ''firstName'' and ''lastName'', an entity Person that uses the embeddable as '''EmbeddedId''' and an entity ''MedicalHistory'' that uses the same emebeddable as '''EmbeddedId'''. When the '''One-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''MedicalHistory'' entity and as target the ''Person'' entity. When the new connection is created, a new attribute ''patient'' shall be added in the ''MedicalHistory'' entity, all of the attributes of the embeddable class shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
 +
 
 +
[[Image: OnetoOneSameEmbeddedIds.jpg ]]
  
 
==== The parent entity has a composite primary key (IdClass) and the dependent entity uses IdClass to represent a composite key. ====
 
==== The parent entity has a composite primary key (IdClass) and the dependent entity uses IdClass to represent a composite key. ====
Line 137: Line 279:
 
   String lastName;
 
   String lastName;
 
}
 
}
 +
 
@Entity
 
@Entity
 
@IdClass(EmployeeId.class)
 
@IdClass(EmployeeId.class)
Line 146: Line 289:
 
public class DependentId {
 
public class DependentId {
 
   String name; // matches name of attribute
 
   String name; // matches name of attribute
   EmployeeId emp; //matches name of attribute and type of Employee PK
+
   EmployeeId employee; //matches name of attribute and type of Employee PK
 
}
 
}
  
Line 160: Line 303:
 
   })
 
   })
 
   @ManyToOne
 
   @ManyToOne
   Employee emp;
+
   Employee employee;
 
}
 
}
 
</source>
 
</source>
  
  
In the JPA editor it means that there shall be an entity ''Employee'' with composite primary key (IdClass) that has two fields ''firstName'' and ''lastName''and an entity ''Dependent''with composite primary key ''name''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the IdClass). If such an attribute is missing it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
+
In the JPA editor it means that there shall be an entity ''Employee'' with composite primary key (IdClass) that has two fields ''firstName'' and ''lastName''and an entity ''Dependent''with composite primary key ''name''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the IdClass). If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
  
 
[[Image: ManyToOneIdClassDepIdClass.jpg]]
 
[[Image: ManyToOneIdClassDepIdClass.jpg]]
Line 178: Line 321:
 
   String lastName;
 
   String lastName;
 
}
 
}
 +
 
@Entity
 
@Entity
 
@IdClass(EmployeeId.class)
 
@IdClass(EmployeeId.class)
Line 200: Line 344:
 
   })
 
   })
 
   @ManyToOne
 
   @ManyToOne
   Employee emp;
+
   Employee employee;
 
}
 
}
 
</source>
 
</source>
  
  
In the JPA editor it means that there shall be an entity ''Employee'' with composite primary key (IdClass) that has two fields ''firstName'' and ''lastName'',  an embeddable ''DependentId'' and an entity ''Dependent'' that uses the emebeddable as '''EmbeddedId'''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the parent's IdClass). If such an attribute is missing it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as id by the parent entity's attribute in the embeddable class:
+
In the JPA editor it means that there shall be an entity ''Employee'' with composite primary key (IdClass) that has two fields ''firstName'' and ''lastName'',  an embeddable ''DependentId'' and an entity ''Dependent'' that uses the emebeddable as '''EmbeddedId'''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the parent's IdClass). If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as id by the parent entity's attribute in the embeddable class:
  
 
[[Image: ManyToOneIdClassEmbeddedId.jpg]]
 
[[Image: ManyToOneIdClassEmbeddedId.jpg]]
 
 
  
 
==== The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses IdClass to represent a composite key. ====
 
==== The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses IdClass to represent a composite key. ====
Line 221: Line 363:
 
   String lastName;
 
   String lastName;
 
}
 
}
 +
 
@Entity
 
@Entity
 
public class Employee {
 
public class Employee {
 
   @EmbeddedId
 
   @EmbeddedId
   EmployeeId empId;
+
   EmployeeId Id;
 
}
 
}
  
 
public class DependentId {
 
public class DependentId {
 
   String name; // matches name of @Id attribute
 
   String name; // matches name of @Id attribute
   EmployeeId emp; // matches name of @Id attribute and type of embedded id of Employee
+
   EmployeeId employee; // matches name of @Id attribute and type of embedded id of Employee
 
}
 
}
  
Line 236: Line 379:
 
public class Dependent {
 
public class Dependent {
 
   @Id
 
   @Id
  @Column(name="dep_name") // default column name is overridden
 
 
   String name;
 
   String name;
 
   @Id
 
   @Id
Line 244: Line 386:
 
   })
 
   })
 
   @ManyToOne
 
   @ManyToOne
   Employee emp;
+
   Employee employee;
 
}
 
}
 
</source>
 
</source>
  
  
In the JPA editor it means that there shall be an embeddable ''EmployeeId'' with two attributes ''firstName'' and ''lastName'', an entity ''Employee'' that issed this embeddable class as composite primary key (EmbeddedId) and an entity ''Dependent'' with composite primary key ''name''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the EmbeddedId class). If such an attribute is missing it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
+
In the JPA editor it means that there shall be an embeddable ''EmployeeId'' with two attributes ''firstName'' and ''lastName'', an entity ''Employee'' that issed this embeddable class as composite primary key (EmbeddedId) and an entity ''Dependent'' with composite primary key ''name''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the EmbeddedId class). If such an attribute does not exist it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as primary key:
  
 
[[Image: ManyToOneEmbeddedIdDepIdClass.jpg]]
 
[[Image: ManyToOneEmbeddedIdDepIdClass.jpg]]
 +
 +
==== The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses EmbeddedId to represent a composite key. ====
 +
 +
The source code shall look like this:
 +
 +
<source lang="java">
 +
@Embeddable
 +
public class EmployeeId {
 +
  String firstName;
 +
  String lastName;
 +
}
 +
 +
@Entity
 +
public class Employee {
 +
  @EmbeddedId
 +
  EmployeeId empId;
 +
}
 +
 +
@Embeddable
 +
public class DependentId {
 +
  String name;
 +
  EmployeeId empPK; // corresponds to PK type of Employee
 +
}
 +
 +
@Entity
 +
@IdClass(DependentId.class)
 +
public class Dependent {
 +
  @EmbeddedId
 +
  DependentId id;
 +
  @MapsId("empPK")
 +
  @JoinColumns({
 +
      @JoinColumn(name="Employee_firstName", referencedColumnName="firstName"),
 +
      @JoinColumn(name="Employee_lastName", referencedColumnName="lastName")
 +
  })
 +
  @ManyToOne Employee employee;
 +
}
 +
</source>
 +
 +
In the JPA editor it means that there shall be an embeddable ''EmployeeId'' with two attributes ''firstName'' and ''lastName'', an entity ''Employee'' that issed this embeddable class as composite primary key (EmbeddedId), another embeddable ''DependentId'' and another entity ''Dependent'' that uses the second emebeddable as '''EmbeddedId'''. When the '''Many-to-One''' uni-directional relation feature is selected from the '''''Derived Identifiers''''' palette, the connection which will be created shall have as owner the ''Dependent'' entity and as target the ''Employee'' entity. During the creation of the connection it shall be checked if the ''Dependent'' entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the composite primary key of the ''Employee'' entity (the same type as the EmbeddedId class). If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the ''Dependent'' entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the '''JoinColumns''' annotation. The newly created attribute shall be mapped as id by the parent entity's attribute in the embeddable class:
 +
 +
[[Image: ManyToOneEmbeddedIdDepEmbeddedId.jpg]]

Latest revision as of 09:13, 7 January 2013

Contents

Overview

Each entity must have a primary key. Primary key must be defined exactly once in an entity hierarchy by one of the following approaches:

  • on the entity class himself;
  • on the entity class that is the root of the entity hierarchy;
  • on a mapped superclass that is a (direct or indirect) superclass of all entity classes in the entity hierarchy.

Derived Ids

When there is an uni- or bi-directional relationship of type one-to-one and many-to-one the owner of the relationship can derive identity from the target entity.

In palette of the JPA diagram editor, there shall be a new section, named Derived Identifiers, which shall contain the following palette entries: One-to-One uni- and bi-directional and Many-to-One uni- and bi-directional relationships:

DerivedIdentifiersPalette.jpg

Configuring Derived Identifiers

The identifier in either of the entities might be composed of one or a plurality of attributes. The relationship from the dependent entity to the parent entity might make up the entire derived identifier, or there might be additional state in the dependent entity that contributes to it. One of the entities might have a simple or compound primary key, and in the compound case might have an id class or an embedded id class. All of these factors combine to produce a multitude of scenarios, each of which requires slightly different configurations.

The parent entity has a simple primary key and the dependent entity has a single primary key attribute which is mapped by the relationship attribute.

The source code shall look like this:

@Entity
public class Person {
   @Id String ssn;
}
 
@Entity
public class MedicalHistory {
   // default join column name is overridden
   @Id
   @OneToOne
   @JoinColumn(name="Person_ssn")
   Person patient;
}

In the JPA editor it means that there shall be an entity Person with primary key ssn and an entity MedicalHistory without any primary keys. When the One-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the MedicalHistory entity and as target the Person entity. When the new connection is created, a new attribute patient shall be added in the MedicalHistory entity and shall be mapped as primary key:

OneToOneSinglePK1.jpg

The parent entity has a simple primary key and the dependent entity has a single primary key attribute corresponding to the relationship attribute.

The source code shall look like this:

@Entity
public class Person {
   @Id String ssn;
}
 
@Entity
public class MedicalHistory {
   @Id String id; // overriding not allowed
   // default join column name is overridden
   @MapsId
   @JoinColumn(name="Person_ssn")
   @OneToOne Person patient;
}

In the JPA editor it means that there shall be an entity Person with primary key ssn and an entity MedicalHistory with primary key id. When the One-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the MedicalHistory entity and as target the Person entity. When the new connection is created, a new attribute patient shall be added in the MedicalHistory entity and shall be mapped as primary key:

OneToOneSharedPK1.jpg

The parent entity has a simple primary key and the dependent entity uses IdClass to represent a composite key.

The source code shall look like this:

@Entity
public class Employee {
   @Id
   long id;
   String empName;
}
 
public class DependentId {
   String name; // matches name of @Id attribute
   long employee; // matches name of @Id attribute and type of Employee PK
}
 
@Entity
@IdClass(DependentId.class)
public class Dependent {
   @Id String name;
   // id attribute mapped by join column default
   @Id
   @ManyToOne
   Employee employee;
}

In the JPA editor it means that there shall be an entity Employee with primary key id and an entity Dependent with composite primary key name. When the Many-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the Dependent entity and as target the Employee entity. During the creation of the connection it shall be checked if the Dependent entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the primary key of the Employee entity. If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the Dependent entity and shall be mapped as primary key:

ManyToOneDepIdClass1.jpg

The parent entity has a simple primary key and the dependent entity uses EmbeddedId to represent a composite key.

The source code shall look like this:

@Entity
public class Employee {
   @Id
   long id;
}
 
public class DependentId {
   String name;
   long empPK; // corresponds to PK type of Employee
}
 
@Entity
public class Dependent {
   @EmbeddedId
   DependentId id;
   // id attribute mapped by join column default
   @MapsId("empPK") // maps empPK attribute of embedded id
   @ManyToOne
   Employee employee;
}

In the JPA editor it means that there shall be an entity Employee with primary key id, an embeddable DependentId and an entity Dependent that uses the emebeddable as EmbeddedId. When the Many-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the Dependent entity and as target the Employee entity. During the creation of the connection it shall be checked if the Dependent entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the primary key of the Employee entity. If such an attribute does not exists, it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the Dependent entity and shall be mapped as id by the parent entity's attribute name in the embeddable class:

ManyToOneDepEmbeddedId1.jpg

The parent entity has a composite primary key (IdClass) and the dependent entity uses the same class to represent a composite key (IdClass).

The source code shall look like this:

public class PersonId {
   String firstName;
   String lastName;
}
 
@Entity
@IdClass(PersonId.class)
public class Person {
   @Id String firstName;
   @Id String lastName;
}
 
@Entity
@IdClass(PersonId.class)
public class MedicalHistory {
   @Id
   @JoinColumns({
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
   })
   @OneToOne
   Person patient;
}

In the JPA editor it means that there shall be an entity Person with composite primary key (IdClass) that has two fields firstName and lastName and an entity MedicalHistory that has composite key using the same IdClass. When the One-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the MedicalHistory entity and as target the Person entity. When the new connection is created, a new attribute patient shall be added in the MedicalHistory entity, all of the primary key attributes of the parent entity shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as primary key:

OneToOneSameIdClass1.jpg

The parent entity has a composite primary key (IdClass) and the dependent entity uses the same class to represent a composite key (EmbeddedId).

The source code shall look like this:

@Embeddable
public class PersonId {
   String firstName;
   String lastName;
}
 
@Entity
@IdClass(PersonId.class)
public class Person {
   @Id String firstName;
   @Id String lastName;
}
 
@Entity
public class MedicalHistory {
   //all attributes map to relationship: AttributeOverride not allowed
   @EmbeddedId
   PersonId id;
   @MapsId
   @JoinColumns({
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
   })
   @OneToOne
   Person patient;
}

In the JPA editor it means that there shall be an entity Person with composite primary key (IdClass) that has two fields firstName and lastName, an embeddable (the same IdClass) and an entity MedicalHistory that uses the emebeddable as EmbeddedId. When the One-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the MedicalHistory entity and as target the Person entity. When the new connection is created, a new attribute patient shall be added in the MedicalHistory entity and all of the primary key attributes of the parent entity shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as primary key:

OneToOneSameIdClassEmbeddedId.jpg

The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses the same class to represent a composite key (IdClass).

The source code shall look like this:

@Embeddable
public class PersonId {
   String firstName;
   String lastName;
}
 
@Entity
public class Person {
   @EmbeddedId 
   PersonId id;
}
 
@Entity
@IdClass(PersonId.class)
public class MedicalHistory {
   @Id
   @JoinColumns({
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
   })
   @OneToOne
   Person patient;
}

In the JPA editor it means that there shall be an embeddable PersonId with two attributes firstName and lastName, an entity Person that useses the embeddable as EmbeddedId and an entity MedicalHistory that has composite key using the same embeddable as IdClass. When the One-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the MedicalHistory entity and as target the Person entity. When the new connection is created, a new attribute patient shall be added in the MedicalHistory entity, all of the attributes of the embeddable class shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as primary key:

OneToOneSameEmbeddedIdIdClass.jpg

The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses the same class to represent a composite key (EmbeddedId).

The source code shall look like this:

@Embeddable
public class PersonId {
   String firstName;
   String lastName;
}
 
@Entity
public class Person {
   @EmbeddedId
   PersonId id;
}
 
@Entity
public class MedicalHistory {
   //all attributes map to relationship: AttributeOverride not allowed
   @EmbeddedId
   PersonId id;
   @MapsId
   @JoinColumns({
      @JoinColumn(name="Person_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Person_lastName", referencedColumnName="lastName")
   })
   @OneToOne
   Person patient;
}

In the JPA editor it means that there shall be an an embeddable PersonId with two attributes firstName and lastName, an entity Person that uses the embeddable as EmbeddedId and an entity MedicalHistory that uses the same emebeddable as EmbeddedId. When the One-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the MedicalHistory entity and as target the Person entity. When the new connection is created, a new attribute patient shall be added in the MedicalHistory entity, all of the attributes of the embeddable class shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as primary key:

OnetoOneSameEmbeddedIds.jpg

The parent entity has a composite primary key (IdClass) and the dependent entity uses IdClass to represent a composite key.

The source code shall look like this:

public class EmployeeId {
   String firstName;
   String lastName;
}
 
@Entity
@IdClass(EmployeeId.class)
public class Employee {
   @Id String firstName
   @Id String lastName
}
 
public class DependentId {
   String name; // matches name of attribute
   EmployeeId employee; //matches name of attribute and type of Employee PK
}
 
@Entity
@IdClass(DependentId.class)
public class Dependent {
   @Id
   String name;
   @Id
   @JoinColumns({
      @JoinColumn(name="Employee_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Employee_lastName", referencedColumnName="lastName")
   })
   @ManyToOne
   Employee employee;
}


In the JPA editor it means that there shall be an entity Employee with composite primary key (IdClass) that has two fields firstName and lastNameand an entity Dependentwith composite primary key name. When the Many-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the Dependent entity and as target the Employee entity. During the creation of the connection it shall be checked if the Dependent entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the composite primary key of the Employee entity (the same type as the IdClass). If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the Dependent entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as primary key:

ManyToOneIdClassDepIdClass.jpg

The parent entity has a composite primary key (IdClass) and the dependent entity uses EmbeddedId to represent a composite key.

The source code shall look like this:

public class EmployeeId {
   String firstName;
   String lastName;
}
 
@Entity
@IdClass(EmployeeId.class)
public class Employee {
   @Id String firstName
   @Id String lastName
}
 
public class DependentId {
   String name; // matches name of attribute
   EmployeeId empPK; //matches name of attribute and type of Employee PK
}
 
@Entity
public class Dependent {
   @EmbeddedId
   DependentId id;
   @MapsId("empPK")
   @JoinColumns({
      @JoinColumn(name="Employee_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Employee_lastName", referencedColumnName="lastName")
   })
   @ManyToOne
   Employee employee;
}


In the JPA editor it means that there shall be an entity Employee with composite primary key (IdClass) that has two fields firstName and lastName, an embeddable DependentId and an entity Dependent that uses the emebeddable as EmbeddedId. When the Many-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the Dependent entity and as target the Employee entity. During the creation of the connection it shall be checked if the Dependent entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the composite primary key of the Employee entity (the same type as the parent's IdClass). If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the Dependent entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as id by the parent entity's attribute in the embeddable class:

ManyToOneIdClassEmbeddedId.jpg

The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses IdClass to represent a composite key.

The source code shall look like this:

@Embeddable
public class EmployeeId {
   String firstName;
   String lastName;
}
 
@Entity
public class Employee {
   @EmbeddedId
   EmployeeId Id;
}
 
public class DependentId {
   String name; // matches name of @Id attribute
   EmployeeId employee; // matches name of @Id attribute and type of embedded id of Employee
}
 
@Entity
@IdClass(DependentId.class)
public class Dependent {
   @Id
   String name;
   @Id
   @JoinColumns({
      @JoinColumn(name="Employee_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Employee_lastName", referencedColumnName="lastName")
   })
   @ManyToOne
   Employee employee;
}


In the JPA editor it means that there shall be an embeddable EmployeeId with two attributes firstName and lastName, an entity Employee that issed this embeddable class as composite primary key (EmbeddedId) and an entity Dependent with composite primary key name. When the Many-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the Dependent entity and as target the Employee entity. During the creation of the connection it shall be checked if the Dependent entity has a composite primary key (IdClass) and if this IdClass contains an attribute of the same type as the type of the composite primary key of the Employee entity (the same type as the EmbeddedId class). If such an attribute does not exist it shall be added authomatically. When the new connection is created, a new attribute with the same name as the attribute in the IdClass shall be added in the Dependent entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as primary key:

ManyToOneEmbeddedIdDepIdClass.jpg

The parent entity has a composite primary key (EmbeddedId) and the dependent entity uses EmbeddedId to represent a composite key.

The source code shall look like this:

@Embeddable
public class EmployeeId {
   String firstName;
   String lastName;
}
 
@Entity
public class Employee {
   @EmbeddedId
   EmployeeId empId;
}
 
@Embeddable
public class DependentId {
   String name;
   EmployeeId empPK; // corresponds to PK type of Employee
}
 
@Entity
@IdClass(DependentId.class)
public class Dependent {
   @EmbeddedId
   DependentId id;
   @MapsId("empPK")
   @JoinColumns({
      @JoinColumn(name="Employee_firstName", referencedColumnName="firstName"),
      @JoinColumn(name="Employee_lastName", referencedColumnName="lastName")
   })
   @ManyToOne Employee employee;
}

In the JPA editor it means that there shall be an embeddable EmployeeId with two attributes firstName and lastName, an entity Employee that issed this embeddable class as composite primary key (EmbeddedId), another embeddable DependentId and another entity Dependent that uses the second emebeddable as EmbeddedId. When the Many-to-One uni-directional relation feature is selected from the Derived Identifiers palette, the connection which will be created shall have as owner the Dependent entity and as target the Employee entity. During the creation of the connection it shall be checked if the Dependent entity has a composite primary key (EmbeddedId) and if this embeddable class contains an attribute of the same type as the type of the composite primary key of the Employee entity (the same type as the EmbeddedId class). If such an attribute does not exist, it shall be added authomatically. When the new connection is created, a new attribute of the parent entity shall be added in the Dependent entity, the columns of the parent entity shall be obtained and shall be automatically mapped with the JoinColumns annotation. The newly created attribute shall be mapped as id by the parent entity's attribute in the embeddable class:

ManyToOneEmbeddedIdDepEmbeddedId.jpg

Back to the top