Tuesday, 15 March 2011

Hibernate subcriteria with FetchMode doesn't work -



Hibernate subcriteria with FetchMode doesn't work -

i want utilize criteria conditions of 1 many field.

introducing model briefly, 1 item has multiple options. because want item filtered options, , pass json parser (with no session), have options when i( query it.

i have coded below before adding restrictions options, works me fine.

item item = (item)session.createcriteria(item.class) .setfetchmode("options", fetchmode.join) .add(restrictions.eq("id", id)) .uniqueresult();

but when added restrictions options occurs error (exactly, when called function tries 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 :

failed lazily initialize collection of role: options, not initialize proxy - no session

i have added setfetchmode end of sub criteria, result same.

what happened!? tell solution?

i think getting error when trying access options after session closed. according hibernate collections lazy-loaded default. if want fetch options along item have set fetchtype eager (fetchtype.eager) in annotation or mapping file whatever using.

can seek code

item item = (item)session.createcriteria(item.class) .setfetchmode("options", fetchmode.select) .add(restrictions.eq("id", id)) .createcriteria("options").add(restrictions.eq("options.status", itemstatus.able)) .uniqueresult();

hibernate hibernate-criteria

No comments:

Post a Comment