Hibernate @Embeddable and @Embedded Annotation (original) (raw)

Last Updated : 23 Apr, 2026

@Embeddable and @Embedded are JPA annotations used to model reusable value objects within an entity. They allow grouping related fields into a separate class and storing them in the same table as the parent entity without creating a new table.

@Embeddable

@Embeddable is used to define a class whose fields can be embedded into another entity. This class does not exist independently in the database and is always used as part of another entity.

@Embeddable public class Address { private String street; private String city; private String state; private String zip; // getters and setters }

`

@Embedded

@Embedded is used inside an entity to include an @Embeddable class as a field. It ensures that all fields of the embedded object are stored in the same table as the parent entity.

@Entity public class Employee { @Id private int id; private String name; @Embedded private Address address; // getters and setters }

`

**Explanation:

Note: You can also use the @AttributeOverrides to customize the column name mapping.

Java `

@Embedded @AttributeOverrides({ @AttributeOverride(name = "street", column = @column(name = "home_street")) }) private Address address;

`

**Explanation: The above example will map the street property of the Address class to home_street in the database table.

Advantages of @Embeddable and @Embedded annotations

@Embeddable vs @Embedded

@Embeddable @Embedded
Used to define a reusable value class Used to include that value class in an entity
Applied on a class Applied on a field
Does not create a separate table Stores data in the parent table
Cannot exist independently Depends on @Embeddable class
Defines structure of embedded data Uses that structure inside entity
No primary key required Uses parent entity’s primary key