@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.
- No separate table is created for the embedded class
- Both annotations are used together for proper mapping
- Helps organize complex data into reusable components
@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.
- Does not have its own identity or primary key
- Cannot be persisted independently
- Provides a reusable structure for grouping fields
@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.
- Used only within entity classes
- Maps embedded fields to the parent table
- Supports column customization using overrides
@Entity
public class Employee {
@Id private int id;
private String name;
@Embedded private Address address;
// getters and setters
}
Explanation:
- The Address class is marked with @Embeddable, so it can be used inside another class as a value object. The Employee class uses @Embedded to include Address, which allows all its fields to be stored in the same table as Employee.
- When an Employee is saved, the Address fields are stored in the same table with a prefix like address_.
Note: You can also use the @AttributeOverrides to customize the column name mapping.
@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
- Code Reusability: We can reuse the embeddable class in multiple entities, avoiding duplication of code.
- Normalization: It can reduce the number of tables by embedding related fields into a single table, improving simplicity and read performance, though this approach is closer to denormalization than normalization.
- Data Integrity: It ensures data integrity by maintaining the relationship between the embeddable and the containing class.
- Simplicity: It simplifies the development process by reducing the number of classes and tables required to map the data.
- Better Data Modelling: It allows for better data modeling by allowing you to encapsulate the properties of an object within another object, making the data structure more intuitive and easy to understand.
@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 |