我通过onetoone关系加入了3个实体。我的目标是使用match.status != null
的hibernate条件获取实体。如何告诉hibernate不要将algo
实体加入到结果中,应该是(pick.algo = null)。
@Entity
public class Pick {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int pid;
@Column(columnDefinition="DATETIME")
private Date insertTime;
@Column(columnDefinition="DATETIME")
private Date setupResTime;
@OneToOne
private DbMatch match;
@OneToOne
private Algo algo;
@Transient
private Integer algoID;
....
标准查询:
public List<Pick> getPicksHistory(){
Criteria criteria = session.createCriteria(Pick.class);
criteria.add(Restrictions.isNotNull("match.status"));
return criteria.list();
}
答案 0 :(得分:0)
您可以添加别名并将条件应用于别名
Criteria criteria = session.createCriteria(Pick.class);
criteria.createAlias("match", "match", JoinType.INNER_JOIN); //<---
criteria.add(Restrictions.isNotNull("match.status"));
要获取空值,您可以使用例如JoinType.LEFT_OUTER_JOIN
答案 1 :(得分:0)
来自hibernate文档(http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#querycriteria-associations):
Criteria criteria = session.createCriteria(Pick.class);
criteria.createAlias("match", "m");
criteria.add(Restrictions.isNotNull("m.status"));
criteria.setFetchMode("algo", FetchMode.LAZY);
criteria.list();