Editing Derived Ids in JPA Diagram Editor

From Eclipsepedia

Jump to: navigation, search

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