java - Efficient Hibernate criteria for join returning many partial duplicates -
i'm fetching long list of entities refer others refer to... and, @ end, of them refer single user
owner
. not surprising what's queried entities belonging single user
. there more parts duplicated in many rows; actually, small percentage unique data. query seems slow, though gain bit fetching things separately using
criteria.setfetchmode(path, fetchmode.select);
this works in above case, when querying on many users (as admin), gets terrible, hibernate issues separate query every user
, instead of like
select * user id in (?, ?, ..., ?)
or not fetching them @ (which can't worse 1 query per entity). wonder missing?
so instead of fetching lot of redundant data, ran 1+n problem, 1+1 queries do.
- is there way instruct hibernate use right query?
- is there way prevent hibernate fetching owners specifying in criteria (rather putting
fetch=fetchtype.lazy
on field; laziness should query-specific)?
i don't think matters, classes like
class child { @manytoone father father; @manytoone mother mother; ... } class father { @manytoone user owner; ... } class mother { @manytoone user owner; ... }
and query like
createcriteria(child.class) .add(restrictions.in("id", idlist)) .add(restrictions.eq("isdeleted", false)) .createalias("father", "f") .add(restrictions.eq("f.isdeleted", false)) .setfetchmode("f.owner", fetchmode.select) .createalias("mother", "m") .add(restrictions.eq("m.isdeleted", false)) .setfetchmode("m.owner", fetchmode.select) .list();
the important part owner
not used , can proxied. javadoc fetchmode.select
says
fetch eagerly, using separate select
so promises 1+1 querying want rather "using separate select per entity".
unless property declared
@manytoone(fetch=fetchtype.lazy)
, can't change anything
true, @ least time being, until fetch profile capabilities extended provide ability change eager loading lazy.
the default
fetchtype.eager
, stupid, can't overridden
true, , agree bad, in hibernate native api lazy default; jpa mandates to-one associations eager unless explicitly specified otherwise.
using criteria.setfetchmode(path, fetchmode.select) pointless it's no-op (either gets ignored because of non-overridable eagerness of property or property lazy already)!
with should able override other lazy fetch modes. see hhh-980 , this comment 1 of lead hibernate contributors javadoc confusion.
fetching lazily leads default 1+n problem
it has nothing lazy loading specifically, default eager loading if don't fetch eagerly loaded association in same query.
it can controlled via class-level
@batchsize
annotation
you have place on class-level take effect on to-one associations entity; this answer helpful. collection associations (to-many associations entity defined in other entities) have flexibility define separately each association.
Comments
Post a Comment