Cascade type annotaion on an embeddable type

#1

Assume we have the following classes:

@Entity
public class Restaurant
{
    /* @Embedded */ private Location location;
    private int phoneNumber;
}

@Embeddable
public class Location
{
    private double latitude;
    private double longitude;
}

According to the documentation, the @Embedded annotation "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."

So the Location class is annotated as instructed, but what difference will it make if we would annotate the Location field with the @Embedded annotation? And do we have to use this annotation also?

In addition, does CascadeType affects embeddable objects?

#2

No need to specify the Embeddedjavax.persistence.EmbeddableJPA annotationDefines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity.See JavaDoc Reference Page... annotation in this case. You may specify it if you think that it may improve the clarity of your code (as it will be clear that location is embedded and not a separate entity when looking at the Restaurant class with no need to look at Location).

Embedded objects are part of the containing entity objects. Because they are not separate entity objects and not stored in the database in a separate location cascading is not required for the embedded objects themselves.

There could be some specific case in which specifying Embeddedjavax.persistence.EmbeddableJPA annotationDefines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity.See JavaDoc Reference Page... is needed and it is related to cascading. If the embedded object contains a reference to an entity object with cascading annotation, it will be ignored by ObjectDB (this could be changed in the future) unless you annotate that embedded object explicitly as Embeddedjavax.persistence.EmbeddableJPA annotationDefines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity.See JavaDoc Reference Page....

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thanks for the clarification.smiley


Post Reply

To post a reply and/or subscribe to update notifications - please login