aspose file tools*
The moose likes Object Relational Mapping and the fly likes Critial Problem with one-to-one association. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Critial Problem with one-to-one association." Watch "Critial Problem with one-to-one association." New topic
Author

Critial Problem with one-to-one association.

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
In hibernate, presently one-to-one associations are fetched non-lazily...
I know that this is done because, hibernate requires to fire 1 sql to find whether proxy can be created or not...
and if it has to fire 1 sql, then why not to load one-to-one object itself in 1 sql instead of creating proxy of it (in 1 sql).

Consider my scenario..
Product has one-to-one with ProductBasic which has one-to-one with ProductTerm

Now i just queried for List of Products and i got say 300 product records.. (in 1 sql query)
In this case i don;t require ProductBasic and ProductTerm.... but as these are one-to-one associations, there are
compulsorily non-lazily fetched. Thus another 300 + 300 select queries are fired to get ProductBasic and ProductTerm
which is surely unacceptable as there are 600 extra sql's getting fired.. The situation may become worst if i
have many one-to-one associations in my domain tree.

I think hibernate code must be changed so that one-to-one associations should be not compulsorily non-lazily fetched....

One way i could think of it is, if user has not asked to fetch one-to-one association then instead of
loading one-to-one object (or creating proxy) simply null should be returned without firing any SQL...
Later when user asks for this one-to-one association, now fire 1 sql to get this association..

The major advantage of this approach is that if user has not asked for one-to-one association, no extra sql (unnecessary)
queries will be fired... (specially when retriving parent collection - e.q our scenario described above)..

This critial problem will always occur when we fetch any collection in the domain tree...
All it's one-to-one associations will be compulsorily fetched for 'n' records. and as n increases, the problem becomes severe..

As per my knowledge, this problem is so critical and severe, that if not solved, nobody will be able to use hibernate
efficiently (and acceptably) in real J2EE applications and hence i request for an immediate solution for this problem..
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

But you can just set a one-to-one relation to be loaded in a lazy manner. So, basically, you have a choice. You're not forced into eager loading.

-Cameron McKenzie
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
No, you can;t load one-to-one associations in lazy manner... If you even specify to load one-to-one associations lazily, it will be ignored.

https://www.hibernate.org/162.html

Now this is a critical problem...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Sandeep Vaid wrote:No, you can;t load one-to-one associations in lazy manner... If you even specify to load one-to-one associations lazily, it will be ignored.

https://www.hibernate.org/162.html

Now this is a critical problem...


Two things.

That link is based on the mapping having

constraint="true"

That is how I read it.

2. " Thus another 300 + 300 "

If Hibernate eager loads one to one in constraint="true" cases, why would it be another 300 + 300. Wouldn't they be all loaded in that first query?

And if not, why not set the fetching strategy to be "subselect" and then it would be only 3 queries. One for the List of Products, and one for ProductBasics and one for all the ProductTerms.

I think the only way to know for sure is to actually code that scenario, try the different mappings and fetching strategies and see the number of queries that Hibernate actually runs.

After all we are just conjecturing here, including myself.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
I found the problem...
The problem is that i have specified property-ref in one-to-one association as :
I have one-to-one relationship between Product and ProductBasic.

PRODUCT ------> PRODUCTID(PK)
PRODUCT ------> PID, CODE, STARTTIME (Composite-key) and PID is FK

In product.hbm.xml
<one-to-one name="productBasic" cascade="save-update" property-ref="activeProduct" constrained="true">
<formula>PRODUCTID</formula>
</one-to-one>


ProductBasic.hbm.xml as :
<properties name="activeProduct">
<many-to-one name="product" class="Product" insert="false" update="false" lazy="proxy" />
</properties>

Now it will always get ProductBasic (no matter we have specified constrained="true")...
Now Here 2 cases arises :
Case 1: Loading ProductBasic in a different query than that of Product. If i just specify
<one-to-one name="productBasic" cascade="save-update" property-ref="activeProduct" constrained="true">


Case 2: Loading ProductBasic in a same query than that of Product. If i just specify
<one-to-one name="productBasic" cascade="save-update" property-ref="activeProduct" constrained="true" outer-join="true">


I feel in my scenario, Case 2 holds good....
Presently i am doing some R&D on this.. will post you if i find any more interesting thing on this....
Thanks for your help !!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Critial Problem with one-to-one association.