我有以下实体(不完全但提供一般概念):
@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. }
我的问题是尝试使用WebElements
API与HQL来获取Criteria
时
执行以下操作时,我得到一个空列表。
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
你能帮忙找一下我在做错了什么或不同的电话吗?(注意我的偏好是使用Criteria API而不是HQL。
答案 0 :(得分:0)
您正在使用Restrictions.eq而不是Restrictions.in()..因为您正在使用HQL。
答案 1 :(得分:0)
在您的HQL中,您正在创建一个内部联接,这会导致Hibernate获取元素。
在Criteria Query中,您可以使用FetchMode.JOIN使用setFetchMode()
由于您的查询是静态的,我建议使用HQL - 它更容易理解。