简体   繁体   English

如何将休眠子条件限制应用于Java列表?

[英]how to apply hibernate child criteria restrictions to java list?

class A{
     String name;
     List<B> b;
     boolean ra;
}

 class B{
    boolean rb;  
    int a;
        }

i want to add child criteria that everything from List of b will come whhose value of boolean rb is false in class b. 我想添加子条件,以使b列表中的所有内容都变为布尔rb的值在b类中为false。

   Criteria criteria = sessionFactory.getCurrentSession().createCriteria(A.class,"a");
    criteria.add(Restrictions.like("a.name","hi", MatchMode.ANYWHERE);

how add this child criteria that will get some elements from list of b which contain value of rb is false? 如何添加此子条件,它将从b列表中获取包含rb值为false的某些元素?

You can use aliases as described in the Hibernate documentation 您可以按照Hibernate文档中所述使用别名。

List cats = sess.createCriteria(Cat.class)
    .createAlias("kittens", "kt")
    .createAlias("mate", "mt")
    .add( Restrictions.eqProperty("kt.name", "mt.name") )
    .list();

So in your case you want to create alias for ´b´. 因此,您要为“ b”创建别名。

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(A.class,"a");
    criteria.createAlias('b', 'b');
    criteria.add(Restrictions.eq('b.rb', false));
    ...

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM