ObjectDB ObjectDB

Cascade type annotaion on an embeddable type


Assume we have the following classes:

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

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?


No need to specify the Embedded 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 Embedded 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 Embedded.

ObjectDB Support

Thanks for the clarification.smiley



To post on this website please sign in.