-->
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.  [ 2 posts ] 
Author Message
 Post subject: Proposed solution for lazy loading of optional OneToOne
PostPosted: Tue Mar 25, 2008 10:45 am 
Newbie

Joined: Fri Oct 12, 2007 5:18 pm
Posts: 10
Hi,

Here's a proposed solution to the limitations of the OneToOne discussed in the following Hibernate article: http://www.hibernate.org/162.html#A3.

How about introducing a generic wrapper class that would be used when we want a lazy loading OneToOne? For instance:

Code:
@Entity
public EntityA {
 
    private OneToOneWrapper<EntityB> entityB;

    @OneToOne(fetch=LAZY, optional=true)
    public OneToOneWrapper<EntityB> getEntityB() {
        return this.entityB;
    }

    private void setEntityB(OneToOneWrapper<EntityB> entityB) {
        this.entityB = entityB;
    }

}


The contract of such a relationship would be that the getter NEVER returns null. Instead, the wrapper class would contain a "isNull" method that would try to lazy load the entity that it wraps and then tell us if something was found or not.

Code:
EntityA a = em.find(EntityA.class, thePkValue);
if (a.getEntityB().isNull()) { // the call to isNull would trigger a query if the entity is not already loaded
    // do something...
}


To read/write the value, we would use a getValue/setValue defined on the wrapper class:

Code:
EntityA a = em.find(EntityA.class, thePkValue);
EntityB b = a.getEntityB().getValue(); // with the use of generics, no need for casting

...

EntityB newB = new EntityB();
a.getEntityB().setValue(newB);


This is why the setter (setEntityB) is private. We always go through the wrapper class to get/set the value of the relationship. Just like the "isNull" method, the "getValue" and "setValue" method of the wrapper would trigger a query when needed.

The limitations that exist right now is because we have the entity itself as the target of the relationship. If we replace that with a wrapper class that has a clear contract, I think it would solve the problem. What do you guys think?


Top
 Profile  
 
 Post subject: looks good
PostPosted: Tue Apr 22, 2008 2:27 pm 
Newbie

Joined: Sun Dec 24, 2006 10:23 pm
Posts: 11
As a non hibernate expert and after a lot of headeach trying to have this OneTo[Zero|One] relationship lazy I think your solution is the best..
I really don't like to declare ManyToOne with unique='true' to simulate this...
So I hope my post will help yours to be noticed by a real hibernate expert which could tell us why it is or not a good solution.


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

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.