简体   繁体   English

休眠条件:查找嵌套在列表中的属性

[英]Hibernate Criteria : Find property nested in a list

Im trying to build using CriteriaBuilder the following query : 我正在尝试使用CriteriaBuilder构建以下查询:

SELECT * FROM job j, asset a, asset_users au
    where j.JOB_ID = a.ASSET_ID and a.ASSET_ID = au.ASSET_ID and au.USER_ID = 6

Where job has an asset and asset has a list of users... I want to return just the list of jobs which have a asset that contains a given user... 作业有资产的地方,资产有用户的清单...我想只返回资产中包含给定用户的作业的清单...

I saw some guys doing it like that : 我看到有些人这样做:

Session session = this.sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Company.class);
criterion = Restrictions.eq("companyRoles.name", "ADMIN");
criteria.add(criterion);
List<Company> companyList = criteria.list();

Tried to replicate that to criteriabuilder but got no luck. 试图复制到criteriabuilder,但没有运气。 All i was getting is that it couldn't find my user id inside an object list (userList). 我得到的只是它在对象列表(userList)中找不到我的用户ID。 Guess my example is harder because you have to access the object like (job).asset.userList.id . 猜猜我的例子更难,因为您必须访问(job).asset.userList.id之类的对象。 BTW, tried this as well : 顺便说一句,也尝试过这个:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Job> cq = cb.createQuery(Job.class);
Root<Job> jobRoot = cq.from(Job.class);
Join<Job, User> assetJoin = jobRoot.join("asset.userList");
cq.where(assetJoin.get("id").in(id));

got same issue... couldn't find the path. 有同样的问题...找不到路径。

Any help is very much appreciated! 很感谢任何形式的帮助! Thanks 谢谢

I think you are missing a step. 我认为您错过了一步。 You need the next level of joining. 您需要下一级别的加入。

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Job> cq = cb.createQuery(Job.class);
Root<Job> jobRoot = cq.from(Job.class);

// join the asset
Join<Job, Asset> assetJoin = jobRoot.join("asset");

// join the list of users
Join<Asset, User> assetUserJoin = assetJoin.join("userList");

cq.where(assetUserJoin.get("id").in(id));

The type of assetUsrJoin.get("id") is Path<Long> or something similar. assetUsrJoin.get("id")的类型为Path<Long>或类似的名称。 JPA Criteria API JPA标准API

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

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