Does ObjectDB support @Parent for @Embeddable reference to parent?

#1

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

 

--- Webel IT Australia, "The Elements of the Web", Specialists in model-based UML, SysML, Enterprise Java, XML, and Drupal CMS web engineering. Dr Darren Kelly, BSc, PhD, https://www.webel.com.au
#2

Same setter trick also appears at WikiBooks JPA: https://en.wikibooks.org/wiki/Java_Persistence/Embeddables#Example_of_an_Embeddable_object_annotations

@Entity
public class Employee {
  ....
  private EmploymentDetails details;
  ....
  @Embedded
  public EmploymentDetails getEmploymentDetails() {
    return details;
  }
  public void setEmploymentDetails(EmploymentDetails details) {
    this.details = details;
    details.setParent(this);
  }
}
--- Webel IT Australia, "The Elements of the Web", Specialists in model-based UML, SysML, Enterprise Java, XML, and Drupal CMS web engineering. Dr Darren Kelly, BSc, PhD, https://www.webel.com.au
#3

This should probably work with ObjectDB as well, but get methods should be annotated when using property access, rather than the annotated fields, as shown on the first post.

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

This should probably work with ObjectDB as well,

Quick test of setter trick seems to work. Thanks.

but get methods should be annotated when using property access, rather than the annotated fields, as shown on the first post.

Sorry, excuse lazy copy-and-paste from stack forum posting (I had already noticed it).

--- Webel IT Australia, "The Elements of the Web", Specialists in model-based UML, SysML, Enterprise Java, XML, and Drupal CMS web engineering. Dr Darren Kelly, BSc, PhD, https://www.webel.com.au

Post Reply

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