Hibernate loads lazy objects without asking
To keep the transferred data small, I created two objects for my files in the database. The file manager should contain some general information about files and the blob file, including fileId and blob. Often I only need to specify common file transformations. If now I ask for a list of file managers, hibernate unfortunately also selects the blob file (each in one select). So here's my example:
Excerpt from Fileh.class:
@OneToOne(mappedBy = "fileh", targetEntity = Fileblob.class, fetch = FetchType.LAZY)
@org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK })
private Fileblob fileblob;
Excerpt from selection:
Criteria createCriteria = persistentSession.createCriteria(Fileh.class);
List list = createCriteria.list();
ConsoleLog:
Hibernate:
select
this_.`ID` as ID1_78_0_,
this_.`CHDATE` as CHDATE2_78_0_,
this_.`CHUSER` as CHUSER9_78_0_,
this_.`CRDATE` as CRDATE3_78_0_,
this_.`CRUSER` as CRUSER10_78_0_,
this_.`EXTENSION` as EXTENSIO4_78_0_,
this_.`NAME` as NAME5_78_0_,
this_.`SIZE` as SIZE6_78_0_,
this_.`VALID_FROM` as VALID_FR7_78_0_,
this_.`VALID_TO` as VALID_TO8_78_0_
from
CORE.`FILEH` this_
Hibernate:
select
fileblob0_.`ID` as ID1_77_0_,
fileblob0_.`FILEBLOB` as FILEBLOB2_77_0_
from
CORE.`FILEBLOB` fileblob0_
where
fileblob0_.`ID`=?
Hibernate:
select
fileblob0_.`ID` as ID1_77_0_,
fileblob0_.`FILEBLOB` as FILEBLOB2_77_0_
from
CORE.`FILEBLOB` fileblob0_
where
fileblob0_.`ID`=? .....(and so on)
Am I wrong in my assumption that fetch = FetchType.LAZY should be sufficient for my purpose ?
Thanks in advance for hints and suggestions. I just think that I am barking the wrong tree ...
Regards, Vincent
Change: . Diving into the hibernate source code began. In DefaultLoadEventListener.proxyOrLoad(...)
hibernate decides whether the weather is loaded as a proxy or not. In my example, the following option is passed: LoadEventListener.INTERNAL_LOAD_NULLABLE
which results in access to the database. I just don't understand why else ...
Edit2: Problem solved: added optional = false
in OneToOne-abstract:
@OneToOne(mappedBy = "fileh", targetEntity = Fileblob.class, fetch = FetchType.LAZY, optional = false)
@org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK })
private Fileblob fileblob;
source to share
No one has answered this question yet
See similar questions:
or similar: