Honza Zidek Honza Zidek - 3 years ago 94
Java Question

Is the JPA @Embedded annotation mandatory?

I have tried omitting the

annotation and still the fields have been embedded in the table. I cannot find anything which would say that the
annotation is optional.

Is it or is it not optional?

The following code

public class Address {
String city;
String street;

public class Person {
String name;
@Embedded // it seems that it works even if this annotation is missing!?
Address address;

generates always the same table


even if I do not specify

My configuration:

  • JBoss EAP 6.4.0

  • hibernate-jpa-2.0-api-1.0.1.Final-redhat-3.jar

The JPA specification says:



Specifies a persistent field or property of an entity whose value is an instance of an embeddable class. The embeddable class must be annotated as Embeddable.



Specifies a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the entity.

Answer Source

In case of using Hibernate it does not matter if you annotate the field itself (as @Embedded) or if you annotate the referenced class (as @Embeddable). At least one of both is needed to let Hibernate determine the type.

And there is a (implicit) statement about this inside the Hibernate documentation, take a look here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-component

It says:

The Person entity has two component properties, homeAddress and bornIn. homeAddress property has not been annotated, but Hibernate will guess that it is a persistent component by looking for the @Embeddable annotation in the Address class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download