简体   繁体   English

如何创建标准,其中参数是表上集合字段的成员?

[英]How do I create a criteria where the parameter is a member of a collection field on the table?

I have a class that has the following structure: 我有一个具有以下结构的类:

public class Machine {
    private Set<Line> lines;
}

Now what I want to to is create a query with Criteria where I can get all the machines that contain a specific line. 现在,我要使用Criteria创建一个查询,在其中可以获取包含特定行的所有计算机。

I noticed Criteria has a Restrictions.in() but that checks whether a field of the table is in a Collection and I want to do it the other way around. 我注意到Criteria有一个Restrictions.in(),但是它检查表的字段是否在Collection中,而我想用另一种方法来做。

Thanks for helping me out, Cheers, Stef 感谢您的帮助,干杯,Stef

Following Hibernate documentation should help: http://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/querycriteria.html#querycriteria-associations 下列Hibernate文档应该会有所帮助: http : //docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/querycriteria.html#querycriteria-associations

Basically you'll need to create a 2nd criteria that refers to the elements of lines of the Machine class. 基本上,您需要创建一个第二个条件,该条件引用Machine类的各lines元素。 ie

Criteria machineCriteria = session.createCriteria(Machine.class);
Criteria lineCriteria = machineCriteria.createCriteria("lines");
lineCriteria.add(Restrictions.eq("fieldNameOfLineClass", valueOfFieldNameOfLineClass);
machineCriteria.list();

I find this syntax more meaningful as well as more compact. 我发现此语法更有意义且更紧凑。 Presuming that you have the id of a Line instance in your hand this would get you what you want. 假设您手中有Line实例的ID,这将为您提供所需的东西。

    // we have a Long lineId
    Criteria crit = session.createCriteria(Machine.class)
        .createAlias("lines", "line")
        .add(Restrictions.eq("line.id", lineId);

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

相关问题 使用QueryDSL,如何在子项与所有条件匹配的集合上创建表达式? - With QueryDSL, how do you create an expression on a collection where the sub-items match all criteria? 如何使用 Criteria 查询将字符串字段转换为 where 子句中的数字? - How do I convert a String field to a number in a where clause using a Criteria query? 使用Foreign Collection Field创建表 - Create table with Foreign Collection Field 如何对具有Collection返回值的Collection参数进行单元测试? - How do I unit test for a Collection parameter with an Collection return? 如何使用休眠条件创建查询 - How do I create my query with Hibernate Criteria 如何模拟类成员字段的方法? - How do I mock a method of a class' member field? 如何调用采用“集合”的方法 <? extends T> “参数? - How do I invoke a method that takes a “Collection<? extends T>” parameter? 如何将集合中包含的对象映射到字段,反之亦然? 推土机 - How do I map an object contained in a collection to a field and viceversa? Dozer 如何创建自定义字段以将用户凭据存储在REVINFO表中 - How do I create custom field to store user credentials in REVINFO table 给定集合名称(ArrayList、LinkedList 等)和集合中的项目,如何创建任何集合? - How do I create any Collection given the collection name(ArrayList, LinkedList etc) and items in the collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM