Hello Everybody
I bag you for help (it’s a third day of fight with this issue :(
The question is about one-to-zero-or-one association and lazy fetching (fetching on demand).
Let assume that we have 2 tables:
Code:
Person
======
person_id – primary key
name
Note
====
person_id – primary key for this table and foreign key to Person table
text
Additionally there is no strict 1 to 1 relation. Relation between Person and Note is: 1 to (0 or 1). So this is a reason why I cannot use ‘constrained’ HBM.XML mechanism.
I would like to have this:
Code:
class Person
{
...
private Note note;
But when I have a ‘note’ member in Person class (mapped in different ways) Hibernate always generate complex SQL selects involving more than one table (Person table):
Code:
select
...
from
Person
left outer join Note on ...
left outer join Person on ...
where
personId=?
I would expect (and this is my question!) to get here only simple SQL query against Person table and have additional SELECT in case of some operations on note class member.
I know that documentation stands:
Note that if constrained="false", proxying is impossible and Hibernate will eager fetch the association!
But on Hibernate forum I found many posts with hints that instrumentation could helpful for me. But, unfortunately, I don’t know how to use instrumentation in this particular case.
And maybe there is other way to handle such cases.
Note plase, that I would like to have this one-to-one association because it would be useful to use it with conjunction with runtime fetch strategies where at runtime I can decide in my Criteria queries, what I would like to load via JOINs.
Help pliiiiis.
Kind regards,
Adam
Code: