首页 文章

使用查询api与hql获取结果的结果不同

提问于
浏览
0

我有以下实体(不完全但提供了一般性的想法):

@Entity
public class WebElement implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue
   private Long id;

   @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
   private Set<CoreElement> coreElements;

   private String agent;

   // ... omitting const' get/set hashcode equals etc.
}
public class CoreElement implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue
   private Long id;

   private String value;
   // ... omitting const' get/set hashcode equals etc.
}

我的问题是尝试使用 Criteria API与HQL获取 WebElements
执行以下操作时,我得到一个空列表 .

getCurrentSession().createCriteria(WebElement.class)
                        .createCriteria("coreElements").add(
                                        Restrictions.eq("value", value)).list();

但是当执行以下HQL时,我得到了正确的结果 .

select distinct we from WebElement we, in(we.coreElements) core 
                                  where core.value = :inputValue

你能帮我找到我做错了什么或不同的电话吗?
NOTE 我的偏好是使用Criteria API而不是HQL .

2 回答

  • 0

    在您的HQL中,您正在创建一个内部联接,这会导致Hibernate获取元素 .

    在Criteria Query中,您可以使用FetchMode.JOIN来使用setFetchMode()

    由于您的查询是静态的,我建议使用HQL - 它更容易理解 .

  • 0

    您正在使用Restrictions.eq而不是Restrictions.in()..因为您正在使用HQL .

相关问题