java - Hibernate reloading entities in a fetch join -
i having problem hibernate reloading entities in query though being fetched part of main query.
the entities follows (simplified)
class data { @id string guid; @manytoone(fetch = fetchtype.lazy) @notfound(action = notfoundaction.ignore) datacontents contents; } class dataclosure { @id @manytoone(fetch = fetchtype.eager) @fetch(fetchmode.join) @joincolumn(name = "ancestor_id", nullable = false) private data ancestor; @id @manytoone(fetch = fetchtype.eager) @fetch(fetchmode.join) @joincolumn(name = "descendant_id", nullable = false) private data descendant; private int length; }
this modelling closure table of parent / child relationships.
i have set criteria follows
final criteria criteria = getsession() .createcriteria(dataclosure.class, "dc"); criteria.createalias("dc", "a"); criteria.createalias("dc.descendant", "d"); criteria.setfetchmode("a", fetchmode.join); criteria.setfetchmode("d", fetchmode.join); criteria.add(restrictions.eq("d.metadataguid",guidparameter)); criteria.add(restrictions.ne("a.metadataguid",guidparameter));
this results in following sql query
select this_.descendant_id descenda2_21_2_, this_.ancestor_id ancestor3_21_2_, this_.length length1_21_2_, d2_.guid metadata1_20_0_, d2_.name name5_20_0_, a1_.guid metadata1_20_1_, a1_.name name6_20_1_ data_closure this_ inner join data d2_ on this_.descendant_id=d2_.metadata_guid inner join data a1_ on this_.ancestor_id=a1_.metadata_guid d2_.guid=? , a1_.guid<>?
which looks correctly implementing join fetch. when execute
list list = criteria.list();
i see 1 of these entries in sql log per row in result set
result set row: 0 debug loader - loading entity: [data#testguid19] debug sql - select data0_.guid guid1_20_0_, data0_.title title5_20_0_, data data0_ data0_.guid=? hibernate: (omitted) debug loader - result set row: 0 debug loader - result row: entitykey[data#testguid19] debug twophaseload - resolving associations [data#testguid19] debug loader - loading entity: [datacontents#7f1134f890a446bbb47f3eb64c1cf668] debug sql - select datacontents0_.guid guid_12_0_, datacontents0_.isocreationdate isocreat2_12_0_, datacontents datacontents0_ datacontents0_.guid=? hibernate: (omitted)
it looks though datacontents marked lazily loaded, it's being loaded eagerly.
so either want way in query fetch join dataclosure , data , lazily fetch datacontents, or fetch join datacontents if not possible.
edit:
modelling closure table this
class dataclosure { @id @column(name = "ancestor_id", nullable = false, length =36 ) private string ancestorid; @id @column(name = "descendant_id", nullable = false, length =36 ) private string descendantid; @manytoone(fetch = fetchtype.eager) @fetch(fetchmode.join) @joincolumn(name = "ancestor_id", nullable = false) private data ancestor; @manytoone(fetch = fetchtype.eager) @fetch(fetchmode.join) @joincolumn(name = "descendant_id", nullable = false) private data descendant; private int length; }
fixed problem. in other words, having @id annotation on entities other tables seemed cause issue, though there nothing wrong queries generated.
i think problem here might this
@notfound(action = notfoundaction.ignore)
there plenty of google results using causes lazy loading become eager. think bug in hibernate.
adding list of annotations should fix problem
@lazytoone(value=lazytooneoption.no_proxy)
since informs hibernate not try use property later on no proxy required.
Comments
Post a Comment