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..
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 !!