341 words

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

#1
2017-05-01 09:31

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
webel
webel's picture
Joined on 2011-05-27
User Post #129
#2
2017-05-01 09:44

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
webel
webel's picture
Joined on 2011-05-27
User Post #130
#3
2017-05-01 10:13

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)
support
support's picture
Joined on 2010-05-03
User Post #2,752
#4
2017-05-01 13:48

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
webel
webel's picture
Joined on 2011-05-27
User Post #131

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel