有3个表,A,B,C,
关系如下:
A-->List<B>,B属性有time等;
A-->List<C>,C属性有time等
Java代码:
Entity:
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "axxx_id")
@Fetch(FetchMode.SELECT)
public List<B> getB() {
return b;
}
public void setB(List<B> b) {
this.b = b;
}
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "axxx_id")
@Fetch(FetchMode.SELECT)
public List<C> getC() {
return c;
}
public void setC(List<C> c) {
this.c = c;
}
Server:
Criteria countCriteria = entityDao.getCriteria();
Criteria listCriteria = entityDao.getCriteria();
...
//*********************start***************************
//加条件 当类为一时可以这样
{
countCriteria.createAlias("B", "b");
listCriteria.createAlias("B", "b");
countCriteria.add(Restrictions.le("b.time", time));
listCriteria.add(Restrictions.le("b.time", time));
}
//********************end***************************
//现在类为一个List型的,该怎么加条件??
listCriteria.setFirstResult((pageNo-1)*pageSize);
listCriteria.setMaxResults(pageSize);
List<A> items = listCriteria.list();
hibernate查询时Criteria怎么根据两个time进行查询?
问题已解决:
底层代码:
@Autowired @Qualifier("sessionFactory") protected SessionFactory sessionFactory; protected Session getSession() { return sessionFactory.getCurrentSession(); } @Override public Criteria getExecutableCriteria(){ DetachedCriteria dc = DetachedCriteria .forClass(this.entityClass); return dc.getExecutableCriteria(getSession()); }
service调用代码:
Criteria listCriteria = entityDao.getExecutableCriteria(); listCriteria.createAlias("A","a",JoinType.LEFT_OUTER_JOIN); listCriteria.createAlias("B","b",JoinType.LEFT_OUTER_JOIN); listCriteria.add(Restrictions.or(Restrictions.lt("a.time", time, MatchMode.ANYWHERE),Restrictions.lt("b.time", time, MatchMode.ANYWHERE))); ... ...