Embeddable
.
If a field or property of an entity class is annotated
EmbeddedId
, then no other field or property of the
entity may be annotated Id
or EmbeddedId
,
and the entity class must not declare an IdClass
.
The embedded primary key type must implement
Annotation.equals(java.lang.Object)
and Annotation.hashCode()
, defining value
equality consistently with equality of the mapped primary
key of the database table.
The AttributeOverride
annotation may be used to
override the column mappings declared within the embeddable
class.
The MapsId
annotation may be used in conjunction
with the EmbeddedId
annotation to declare a derived
primary key.
If the entity has a derived primary key, the
AttributeOverride
annotation may only be used to
override those attributes of the embedded id that do not
correspond to the relationship to the parent entity.
Relationship mappings defined within an embedded primary key type are not supported.
Example 1:
@Entity
public class Employee {
@EmbeddedId
protected EmployeePK empPK;
...
}
public record EmployeePK(String empName, Date birthDay) {}
Example 2:
@Embeddable
public class DependentId {
String name;
EmployeeId empPK; // corresponds to primary key type of Employee
}
@Entity
public class Dependent {
// default column name for "name" attribute is overridden
@AttributeOverride(name = "name", column = @Column(name = "dep_name"))
@EmbeddedId
DependentId id;
...
@MapsId("empPK")
@ManyToOne
Employee emp;
}
- Since:
- 1.0
- See Also: