简体   繁体   中英

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. I want to be able to access B's long type field in a criteria query.

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?

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.

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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