首页 新闻 会员 周边 捐助

hibernate查询时Criteria怎么根据级联的两个类里的属性进行查询?

0
悬赏园豆:20 [已解决问题] 解决于 2015-11-12 09:35

有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进行查询?

拥抱天空的风的主页 拥抱天空的风 | 初学一级 | 园豆:119
提问于:2015-11-11 18:19
< >
分享
最佳答案
0

问题已解决:

  底层代码: 

    @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)));
... ...
拥抱天空的风 | 初学一级 |园豆:119 | 2015-11-12 09:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册