The @Embeddable annotation marks a class whose fields are stored as columns of the owning entity's table instead of in a table of their own. You then include it in an entity with a field annotated @Embedded (or just the type, since the annotation on the class is enough). Use it for value objects such as an address, money amount or date range that belong to one entity and have no identity of their own.
The annotation is part of Jakarta Persistence, so in Spring Boot 3 and later it lives in the jakarta.persistence package, and in older code in javax.persistence.
An embeddable is a persistent value type. It is not an entity, so it has no @Id, no table and no lifecycle independent of its owner. Its attributes are mapped to columns in the table of the entity that contains it. Two entities that embed the same class each get their own copy of the columns in their own tables.
That makes embeddables a good fit for domain value objects. Compared with copying the same four address fields into every entity, you define them once and reuse them.
The employee table gets the columns id, name, street, city and postal_code (with Spring Boot's default naming strategy). There is no address table and no join.
You can query the embedded fields in JPQL with a path, for example select e from Employee e where e.address.city = :city.
When one entity embeds the same type twice, or when you need different column names, use @AttributeOverride:
Without overrides, two embedded fields of the same type would map to the same column names and the schema would be invalid.
@Embeddable and must not have an @Id.protected.NULL in the database, the provider loads the field as null, not as an empty object. Handle that in your code.An @Embeddable is also how you define a composite key with @EmbeddedId. In that case the class must implement equals and hashCode and should be Serializable:
| Question | Embeddable | Entity |
|---|---|---|
| Has its own identity | No | Yes |
| Own table | No, columns in the owner's table | Yes |
| Shared by several owners | No, each owner has a copy | Yes, by reference |
| Lifecycle | Same as the owner | Independent |
Choose an entity when other rows must reference it or when it changes independently. Choose an embeddable for pure value objects.
No. The @Embeddable annotation on the class is enough for the provider to treat the field as embedded. @Embedded is allowed and often added for clarity.
Yes. Collections of basic values or embeddables in an embeddable are mapped with @ElementCollection, and they use a separate collection table.
When all of its columns are NULL, the provider returns null for the embedded field. Initialise it in your own code if you need a non-null object.
Use jakarta.persistence with Spring Boot 3 and later. Use javax.persistence only on older Spring Boot 2 projects.
@Embeddable lets you group related attributes into a reusable value object that is stored in the owner's table. Use @Embedded and @AttributeOverride to place and rename the columns, use @EmbeddedId for composite keys, and remember that an all-null embedded object loads as null.