Hibernate Criteria: Emergesel loading the Manytomany collection

My question seems to be simple, but I can’t seem to find a valid answer. I have a Hibernate entity which has a ManyToMany association with another entity, which is obtained lazily by default I want to create a Criteria that will return entities and eagerly load the ManyToMany association.

This is the entity in question (irrelevant parts removed):

@Entity
public class Order {

@ManyToMany
private List items;

@ ManyToMany
private List discountEntries;

...

}

The following are my notable attempts and results so far :

Criteria criteria = getSession().createCriteria(Order.class)
.setFetchMode("items", FetchMode.SELECT)
.setFetchMode( "discountEntries", FetchMode.SELECT);
criteria.list();

– load items lazily

Criteria criteria = getSession ().createCriteria(Order.class)
.setFetchMode("items", FetchMode.JOIN)
.setFetchMode("discountEntries", FetchMode.JOIN);
criteria.list();

-Cannot retrieve multiple Baggage

Criteria criteria = getSession().createCriteria(Order.class)
.createAlias("items", "items", CriteriaSpecification.INNER_JOIN)
.setFetchMode("items", FetchMode.JOIN)
.createAlias("discountEntries", "discountEntries", CriteriaSpecification.INNER_JOIN)
.setFetchMode("discountEntries", FetchMode.JOIN);
criteria.list();

– returns an empty list

Edit: added another ManyToMany association because this seems to be part of the problem.

Use LAZY settings to get the collection and merge:

List list = criteria.list();
Hibernate.initialize(list);

This will initialize immediately and only works in this case.

My question seems simple, but I can’t seem to find a valid answer. I have a Hibernate entity that has a ManyToMany association with another entity, which is obtained lazily by default. I want to create a Criteria and it will return Entity, and eagerly loaded the ManyToMany association.

This is the entity in question (irrelevant parts removed):

@ Entity
public class Order {

@ManyToMany
private List items;

@ManyToMany
private List discountEntries;

...

}

The following are my notable attempts and results so far:

Criteria criteria = getSession().createCriteria(Order.class)
.setFetchMode("items", FetchMode.SELECT)
.setFetchMode("discountEntries", FetchMode.SELECT);
criteria.list();

– load items lazily

Criteria criteria = getSession().createCriteria(Order.class)
.setFetchMode("items", FetchMode.JOIN)
.setFetchMode(" discountEntries", FetchMode.JOIN);
criteria.list();

– Cannot retrieve multiple bags

Criteria criteria = getSession( ).createCriteria(Order.class)
.createAlias("items", "items", CriteriaSpecification.INNER_JOIN)
.setFetchMode("items", FetchMode.JOIN)
.createAlias("discountEntries", "discountEntries", CriteriaSpecification.INNER_JOIN)
.setFetchMode("discountEntries", FetchMode.JOIN);
criteria.list();

– Back An empty list

Edit: Added another ManyToMany association, because this seems to be part of the problem.

Use the LAZY setting to get the collection And:

List list = criteria.list();
Hibernate.initialize(list);

This will initialize immediately , Only applicable in this case.

Leave a Comment

Your email address will not be published.