JPA primary key @Id,@IdClass,@Embeddable,@EmbeddedId
1. Automatic primary key
By default, the primary key is a continuous 64-bit number (long), which is automatically set by ObjectDB for each new entity object stored in the database.
The primary key of the first entity object in the database is 1, the primary key of the second entity object is 2, etc.
When an entity object is deleted from the database, the primary key value is not recycled.
The primary key value of an entity can be accessed by declaring a primary key field:
@Entity public class Project { @Id @GeneratedValue long id; // still set automatically }
-
@id
Tag marks the field as a primary key field. When a primary key field is defined, the primary key value will be automatically injected into the field by ObjectDB. -
@generatedvalue
Comment specifies that the primary key is automatically allocated by ObjectDB
2. Apply to set the primary key
If an entity has a primary key field without the @generatedvalue tag, no automatic primary key value is generated and the application is responsible for setting the primary key by initializing the primary key field. This must be done before any attempt to persist the entity object.
@Entity public class Project { @Id long id; // must be initialized by the application }
The primary key fields set by the application can have the following types:
● Primitive types: boolean, byte, short, char, int, long, float, double.
● Package types in packages: Byte, Short, Character, Integer, Long, Float, Double.
● , .
● .
● , , , .
● Enumeration type
● Reference an entity object
3. Compound primary key
A composite primary key consists of multiple primary key fields. Each primary key field must be one of the supported types listed above.
For example, the primary key of the following project entity class consists of two fields:
@Entity @IdClass() public class Project { @Id int departmentId; @Id long projectId; }
When an entity has multiple primary key fields, JPA needs to define a special ID class that is attached to the entity class using the @idclass annotation. The ID class reflects the primary key field, and its object can represent the primary key value:
Class ProjectId { int departmentId; long projectId; }
ObjectDB does not force the definition of ID classes. However, if the entity object must retrieve the entity object as the primary key shown in the Retrieve Entity section, then the ID class is required.
4. Embedded primary key
Another way to represent composite primary keys is to use embedible classes:
@Entity public class Project { @EmbeddedId ProjectId id; } @Embeddable Class ProjectId { int departmentId; long projectId; }
The primary key field is defined in the embedible class.
The entity contains a separate primary key field annotated with @EmbeddedId and contains an instance of the embedable class.
When using this form, no separate ID class is defined because the embedded class itself can represent the complete primary key value.
The difference between @EmbeddedId and @IdClass
@idClass
Make the composite primary key class a non-embedded class, use the @IdClass annotation to specify a composite primary key class (usually composed of two or more primitive types or JDK object types). When mapping from the original database (at this time the database key consists of multiple columns), a compound primary key will usually appear.
The composite primary key class has the following characteristics:
- It is a normal old Java object (POJO) class.
- It must be public, and there must be a public parameterless constructor.
- If property-based access is used, the properties of the primary key class must be public or protected.
- It must be serializable.
- It must define equals and hashCode methods.
- The semantics of value equality for these methods must be consistent with the database equality of the database type to which the key maps.
- The type and name of its field or attribute must correspond to the type and name of the entity primary key field or attribute annotated with @Id.
package ; import ; public class SysUserRoleId implements Serializable{ /** * */ private static final long serialVersionUID = 2606793267849167078L; private Long userId; private Long roleId; @Override public int hashCode(){ int result = 1; result = ()+(); return result; } @Override public boolean equals(Object obj){ if(obj == null){ return false; } if(this == obj){ return true; } if(getClass() != ()){ return false; } final SysUserRoleId other = (SysUserRoleId) obj; if(().equals() && ().equals()){ return true; } return false; } public Long getUserId() { return userId; } public void setUserId(Long userId) { = userId; } public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { = roleId; } }
package ; import ; import ; import ; import ; import ; @Entity @Table(name="SYS_USER_ROLE") @IdClass() public class SysUserRole { private Long userId; private Long roleId; public SysUserRole(){ } public SysUserRole(Long userId,Long roleId){ = userId; = roleId; } @Id @Column(name="user_id") public Long getUserId() { return userId; } public void setUserId(Long userId) { = userId; } @Id @Column(name="role_id") public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { = roleId; } }
@EmbeddedId
Make the composite primary key class an embedded class owned by an entity
Use the @EmbeddedId annotation to specify an embedded composite primary key class owned by an entity (usually composed of two or more primitive types or JDK object types). When mapping from the original database (at this time the database key consists of multiple columns), a compound primary key will usually appear.
The composite primary key class has the following characteristics:
- It is a normal old Java object (POJO) class.
- It must be public, and there must be a public parameterless constructor.
- If property-based access is used, the properties of the primary key class must be public or protected.
- It must be serializable.
- It must define equals and hashCode methods.
- The semantics of value equality for these methods must be consistent with the database equality of the database type to which the key maps.
package ; import ; import ; @SuppressWarnings("serial") public class SysOrganizationRolePKId implements Serializable{ private Long organizationId; private Long roleId; @Column(name="organization_id") public Long getOrganizationId() { return organizationId; } public void setOrganizationId(Long organizationId) { = organizationId; } @Column(name="role_id") public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { = roleId; } }
package ; import ; import ; import ; import ; @Entity @SuppressWarnings("serial") @Table(name="SYS_ORGANIZATION_ROLE") public class SysOrganizationRole implements Serializable{ private SysOrganizationRolePKId sysOrganizationRolePKId; @EmbeddedId public SysOrganizationRolePKId getSysOrganizationRolePKId() { return sysOrganizationRolePKId; } public void setSysOrganizationRolePKId( SysOrganizationRolePKId sysOrganizationRolePKId) { = sysOrganizationRolePKId; } }
The above is personal experience. I hope you can give you a reference and I hope you can support me more.