AFAIK JPA does not directly support an @Embeddable referencing the parent in which it is @Embedded.
But Hibernate has a special @Parent for it (see Entity extensions 2.4.3.4.@Parent).
@Entity public class Person { @Embeddable public Address address; ... } @Embeddable public class Address { @Parent public Person owner; ... }
Is there a safe and recommended way of achieving this in ObjectDB ?
I saw this trick (or similar) via a setter, but not sure whether it is safe under ObjectDB instrumentation:
@Entity class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Access(AccessType.PROPERTY) @Embedded private Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; this.profile.setUser(this); } // ... }
@Embeddable class Profile implements Serializable { User user; public void setUser(User user) {..} setURL(String url) { if (user.active() ) { // for this kind of usage } } // .. other properties .. }
Visit also: http://stackoverflow.com/questions/5060891/jpa-how-can-an-embeddable-object-get-a-reference-to-its-owner