Annotation Interface Embedded


@Target(FIELD) @Retention(RUNTIME) public @interface Embedded
Indicates an embedded property.

The annotated field must be a member of an Entity annotated class. The type of the field must be a class annotated with Embeddable.

The Embedded annotation allows for embedding value objects within entities, enabling composition of entities from smaller, reusable components.

Basic Usage

 @Embeddable
 public class Address {
     String street;
     String city;
     String zipCode;
 }

 @Entity
 public class Employee {
     @Id
     Integer id;

     String name;

     @Embedded
     Address address;
 }
 

Using Prefix

 @Entity
 public class Person {
     @Id
     Integer id;

     @Embedded(prefix = "home_")
     Address homeAddress;

     @Embedded(prefix = "work_")
     Address workAddress;
 }
 

Using Column Overrides

 @Entity
 public class Customer {
     @Id
     Integer id;

     @Embedded(columnOverrides = {
         @ColumnOverride(name = "street", column = @Column(name = "BILLING_STREET")),
         @ColumnOverride(name = "city", column = @Column(name = "BILLING_CITY")),
         @ColumnOverride(name = "zipCode", column = @Column(name = "BILLING_ZIP"))
     })
     Address billingAddress;

     @Embedded(columnOverrides = {
         @ColumnOverride(name = "street", column = @Column(name = "SHIPPING_STREET")),
         @ColumnOverride(name = "city", column = @Column(name = "SHIPPING_CITY")),
         @ColumnOverride(name = "zipCode", column = @Column(name = "SHIPPING_ZIP", updatable = false))
     })
     Address shippingAddress;
 }
 
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Column overrides for specific properties within the embedded object.
    The prefix for column names of the embedded properties.
  • Element Details

    • prefix

      String prefix
      The prefix for column names of the embedded properties.

      When specified, the prefix is prepended to the column names of all properties within the embedded object. This is useful when embedding the same embeddable type multiple times in an entity.

      For example, if an embeddable has a property mapped to column "street" and the prefix is "home_", the resulting column name will be "home_street".

      Returns:
      the column name prefix
      Default:
      ""
    • columnOverrides

      ColumnOverride[] columnOverrides
      Column overrides for specific properties within the embedded object.

      This allows fine-grained control over the column mappings of individual properties in the embeddable. Each ColumnOverride specifies a property name and its corresponding column definition, which overrides the default column mapping for that property.

      Column overrides are particularly useful when:

      • You need to map properties to columns with names that don't follow the standard naming convention
      • You want to control column attributes (insertable, updatable, quote) for specific properties
      • Different instances of the same embeddable require different column configurations

      Note that column overrides take precedence over the prefix() attribute. If both are specified, the column override will be used for the specified properties, while the prefix will be applied to all other properties.

      Returns:
      an array of column overrides for properties in the embedded object
      See Also:
      Default:
      {}