Annotation Interface Embeddable


@Documented @Target(TYPE) @Retention(RUNTIME) public @interface Embeddable
Declares a type whose instances are stored as an intrinsic part of an owning entity, sharing the identity of the entity. A single embeddable type may be used as the type of multiple persistent fields or properties, across several entities, and so distinct instances of an embeddable type might have owning entities of completely unrelated entity types.

The annotated type must:

  • be a non-abstract, non-final top-level class or static inner class, or a Java record type,
  • have a public or protected constructor with no parameters, unless it is a record type, and
  • have no final methods or persistent instance variables.

An enum or interface may not be designated as an embeddable type.

An embeddable class does not have its own table. Instead, the state of an instance is stored in the table or tables mapped by the owning entity.

The persistent fields and properties of an embeddable class are mapped using the same mapping annotations used to map entity classes, and may themselves hold instances of embeddable types. An embeddable class may even declare an association from its owning entity to another entity.

However, an embeddable class may not have a field or property annotated Id or EmbeddedId.

Fields or properties of an embeddable class are persistent by default. The Transient annotation or the Java transient keyword must be used to explicitly declare any field or property of an embeddable class which is not persistent.

Example 1:

@Embeddable
public class EmploymentPeriod {
    @Temporal(DATE) java.util.Date startDate;
    @Temporal(DATE) java.util.Date endDate;
    ...
}

Example 2:

@Embeddable
public class PhoneNumber {
    protected String areaCode;
    protected String localNumber;
    @ManyToOne
    protected PhoneServiceProvider provider;
    ...
}

@Entity
public class PhoneServiceProvider {
    @Id
    protected String name;
    ...
}

Example 3:

@Embeddable
public class Address {
    protected String street;
    protected String city;
    protected String state;
    @Embedded
    protected Zipcode zipcode;
}

@Embeddable
public class Zipcode {
    protected String zip;
    protected String plusFour;
}
Since:
1.0
See Also: