I have a parent entity with a @ManyToMany(fetch=FetchType.EAGER) child collection.
I need to load only first record i found of Parent entity, so i load it using this criteria:
session.createCriteria(Parent.class).setMaxResult(1).uniqueResult();
Work fine, but the limit is applied to child collection too, and this is very bad.
How can i get only first record of parent, but all record of its child?
Thanks
Just mark the collection of children as
fetch = FetchType.LAZY
, don't fetch it in the query and initialize the collection after the query if necessary:If you really want to keep the association as eager fetched (which is a bad idea, IMO), then only load the ID of the parent in the query, and then get the parent: