• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Critial Problem with one-to-one association.

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!

reply
    Bookmark Topic Watch Topic
  • New Topic