简体   繁体   English

在条件查询中从父对象获取子对象的字段。 (休眠/持续)

[英]Getting the field of a child object from a parent object in criteria query. (Hibernate/Persistence)

I have an object (A), that has a field that contains another object (B), and B has a field of long type. 我有一个对象(A),它的字段包含另一个object (B),而B有一个long型字段。 I want to be able to access B's long type field in a criteria query. 我希望能够在条件查询中访问B的long类型字段。

In the example code, we see how to get the field of an object, but is there a way to get objectA.objectB.field in a similar way as the example code looks like? 在示例代码中,我们看到了如何获取对象的字段,但是有没有一种方法可以像示例代码所示那样获取objectA.objectB.field呢?

I have found this example code: 我找到了以下示例代码:

CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class );
Root<Person> personRoot = criteria.from( Person.class );
criteria.selec(1)t( builder.max( personRoot.get( Person_.age ) ) );
criteria.where( builder.equal( personRoot.get( Person_.eyeColor ), "brown" ) );
Integer maxAge = em.createQuery( criteria ).getSingleResult();

通过指定联接类型从根创建联接

Join<ObjectA, ObjectB> objectBJoin = personRoot.join("objectB", JoinType.INNER);

The answer is we can use multiple get functions inside of each. 答案是我们可以在每个内部使用多个get函数。

Example: 例:

criteria.where(builder.equal(ParentClass.get("ChildClass").get("FieldOfChildClass"), "value" ));

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

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