Annotation Interface EmbeddedId
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,
except in the special case where the EmbeddedId
represents the composite primary key of a parent entity
in an entity with a derived identity represented as an
IdClass.
The embedded primary key type must implement
Annotation.equals(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 PaycheckId {
int period;
EmployeeId empPK; // corresponds to primary key type of Employee
}
@Entity
public class Paycheck {
// default column name for 'period' attribute is overridden
@AttributeOverride(name = "period", column = @Column(name = "pay_period"))
@EmbeddedId
PaycheckId id;
...
@MapsId("empPK")
@ManyToOne
Employee emp;
}
- Since:
- 1.0
- See Also: