开发者

Hibernate Criteria: Eagerly load ManyToMany collection

开发者 https://www.devze.com 2022-12-15 09:48 出处:网络
My proble开发者_运维百科m seems pretty straightforward to me, but I just can\'t seem to find an answer that works. I have a Hibernate Entity which has a ManyToMany association with another Entity, whi

My proble开发者_运维百科m seems pretty straightforward to me, but I just can't seem to find an answer that works. I have a Hibernate Entity which has a ManyToMany association with another Entity, which is fetched lazily by default. I want to create a Criteria which will return the Entity with the ManyToMany association loaded eagerly.

Here is the Entity in question (with irrelevant parts removed):

@Entity
public class Order {

  @ManyToMany
  private List<Item> items;

  @ManyToMany
  private List<Discount> discountEntries;

  ...

}

Here are my notable attempts thus far and the results:

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

- Loads the items lazily

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

- Cannot fetch 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();

- Returns an empty list

EDIT: Added another ManyToMany association as that seems to be part of the problem.


Get the collection with the LAZY setting and :

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

This will initialize it immediately, only for this case.


Did you try: @ManyToMany(fetch = FetchType.EAGER)? what was the result?

0

精彩评论

暂无评论...
验证码 换一张
取 消