简体   繁体   English

休眠条件过滤器内部集合

[英]Hibernate criteria filter inner collection

I have the next classes/tables: 我有以下课程/表:

User 用户

  • id ID
  • name 名称
  • works // list of work works //工作清单

Work 工作

  • id ID
  • name 名称
  • user_id 用户身份

And the following code: 和以下代码:

Criteria criteria = session.createCriteria("User"); 
criteria.list();

returns a list of User s that contain list of Work object assotiated with them (by id=user_id). 返回一个User列表,其中包含与之关联的Work对象列表(通过id = user_id)。

How can I modify this query to get the same list of User but with the follow restriction: List of Work should not includes Work s where name ='fck'? 如何修改此查询以获取相同的User列表,但有以下限制: Work List不应包括名称为'fck'的Work

It's possible, but not wise, because the loaded users wouldn't reflect the actual state of the database : their list of works should contain all their works, and not only some of them. 这是可能的,但不是明智的做法,因为加载的用户不会反映数据库的实际状态:他们的工作清单应包含所有工作,而不仅仅是其中一些工作。 Modifying them could lead to unwanted deletes in the database. 修改它们可能导致数据库中不必要的删除。

I would rather load the works you're interested in, with their associated user : 我宁愿与他们相关的用户一起加载您感兴趣的作品:

Criteria c = session.createCriteria(Work.class, "work");
c.createAlias("work.user", "user");
c.setFetchMode("work.user", FetchMode.JOIN);
c.add(Restrictions.ne("work.name", "fck"));

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

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