-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: proxying nullable one-to-ones
PostPosted: Fri Sep 09, 2011 2:34 pm 
Newbie

Joined: Tue Aug 09, 2011 9:45 am
Posts: 4
A very commonly encountered and annoying limitation of Hibernate is that it can't proxy nullable one-to-one relationships. The rationale is that the reference should be null if there is no associated object but a proxy is always non-null, so Hibernate doesn't know if it can use a proxy until it performs the necessary join, at which point it may as well fetch the associated object.

However, I have seen people hack around this limitation by mapping one-to-ones as one-to-manys and having the application enforce that the collection has at most one element. Such a hack can offer much-improved performance at the cost of some ugliness that can be encapsulated in the domain object (if mapping by field), and thus the benefit sometimes outweighs the cost. It seems that a "better" solution would be to have Hibernate drop a "reference" object into the owning object. The reference object would contain a nullable reference to the associated object and would provide the necessary indirection to avoid needing to load the referenced object upfront.

One principle of Hibernate is that the domain model should consist of POJOs, and thus an org.hibernate.Reference shouldn't pollute domain objects. Instead, why not simply use a reference class built into the standard library, like AtomicReference? Then you could write the following proxyable one-to-one:

Code:
class Owner {
    @OneToOne(nullable = true, mappedBy = "owner")
    private AtomicReference<Owned> owned;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.