Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My initial thought here was to make a class, call it <code>Option</code>, mark it as an <code>@Embeddable</code>, then giving it a reference to a common base class for the set of objects you might want to refer to (call it <code>DomainObject</code>).</p> <p>Doing this though means you might need to give up a couple of things:</p> <ol> <li>"...arbitrary objects..." You can't have a <code>@ManyToOne</code> reference to a fully arbitrary class in Hibernate. It needs to be to a specific Hibernate Entity or Entity hierarchy: ie, <code>DomainObject</code>, otherwise Hibernate has no way of knowing what tables to query at fetch time.</li> <li>"...null values..." Hibernate does some special stuff for Elements (aka <code>@Embeddable</code>). In particular, if the Element only contains null references, then the reference to the Element itself is null.</li> </ol> <p>So you could take that approach, but you'd end up with a base class that you possibly don't want, and some extraneous field in the Element class simply to make sure the Element itself isn't null. Bit of a pain.</p> <p>Instead, I'd recommend keeping your Option class out of Hibernate entirely. In Entities that have Options, use the <a href="http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e1955" rel="nofollow">access type setting</a> so that Hibernate accesses the member via the field, not methods. Implement the getter like so:</p> <pre><code>... @ManyToOne private MyObjectType myObjectReference; public Option&lt;MyObjectType&gt; getMyObject() { return new Option(this.myObjectReference); } ... </code></pre> <p>Another options is to not do field-level access, but instead have inner, private getter/setter for Hibernate to deal with the field, and public getter/setters that wrap it in an Option for the rest of your application to see.</p> <p>Only con here is that the getter has to do some real work as opposed to being a simple POJO method. But I think it's worth it not to twist Hibernate in knots.</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload