I want to use criteria with some conditions of its one to many field.
Introducing my model briefly, one item
has multiple options
.
Because I want to get item
with filtered options
, and pass it to JSON Parser (with no session), I have to get options when I( query it.
I have coded like below before adding restrictions to options, it works for me fine.
Item item = (Item)session.createCriteria(Item.class)
.setFetchMode("options", FetchMode.JOIN)
.add(Restrictions.eq("id", id))
.uniqueResult();
But when I added restrictions to options
it occurs an error (exactly, when a called function tries to access item, searching works fine.)
Item item = (Item)session.createCriteria(Item.class)
.setFetchMode("options", FetchMode.JOIN)
.add(Restrictions.eq("id", id))
.createCriteria("options").add(Restrictions.eq("status", ItemStatus.ABLE))
.uniqueResult();
An error is :
failed to lazily initialize a collection of role: options, could not initialize proxy - no Session
I have added setFetchMode to the end of sub criteria, the result was same.
What happened!? Could you tell the solution?