Maxim's blog
Thoughts on Java, J2EE, outsourcing, consulting and software development in general

Maxim's blog

Hibernate: how to make a one-to-one relationship lazy

 Digg it      Bookmark with del.icio.us

Thu Jul 28, 2005 11:56 pm

[  Mood: Happy ]

First, there's this link:
http://hibernate.org/117.html#A16

However, I recently discovered a problem with Hibernate 3. I have a Master entity that has an optional 1-1 relationship with a Dependent entity. For example, a Person may or may not have single BirthCertificate.
Code:

    <one-to-one name="birthCert" class="com.blah.BirthCertificate" column="bcertId" outer-join="true" lazy="true">
    </one-to-one>

Of course, in Hibernate 3 all associations are lazy by default.
Problem is that when I load instance of Master, Hibernate would still execute unnecessary select statement against BirthCertificate table. I was surprised: if relationship is lazy, Hibernate is supposed to take bcertId from Person table and put it in the proxy. If Person.getBirthCert() is called - only then proxy should inflate the BirthCertificate! More then that, if Person.bcertId is null, why would Hibernate need to run "select from BirthCertifcate where bcertId = null"? And yet it does.

I think it gets confused not only about laziness of the relationship, but also about which table containsthe foreign key.

So, here's the workaround. I use many-to-one association with granularity of 1, which is equivalent of one-to-one or one-to-zero. Here's how:
Code:

<many-to-one unique="true" lazy="true">...

Now Hibernate does not perform select statement unless I touch that relationship.

The Trackback URL for this entry is:

This function is disabled

Page 1 of 1   

Author Message
Guest







PostPosted: Fri Jul 29, 2005 4:35 am    Post subject:    

Did you actually read the FAQ item you linked to?

It explains clearly why an optional (ie. constrained=false) one-to-one association can *not* conceptually be proxied.

>>> why would Hibernate need to run "select from BirthCertifcate where bcertId = null"? And yet it does. <<<

Hibernate does *not* do this (ever).
Back to top
msenin


Joined: 18 May 2005
Posts: 14

View Blog

PostPosted: Sat Jul 30, 2005 12:08 am    Post subject:    

Quote:
Lazy fetching is only conceptually possible for a mandatory association since we have to hit the other table to determine whether the association is null or not!


I disagree. It all depends on where the foreign key is. If the dependent table has reference to the master, then, yes, the dependent table has to be hit. But if master references the dependent, the relationship should know not to touch other table because the master's foreign key column has already been read. Null value would indicate there's no dependent, and vice versa.
Back to top View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:   

forums.supremistic.com Forum Index -> Blogs -> Maxim's blog -> Hibernate: how to make a one-to-one relationship lazy