不为空的条件子查询

发布于 2024-12-19 11:22:19 字数 570 浏览 4 评论 0原文

我想将以下子查询转换为使用 hibernate 子查询:

getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)")
                   .list();
  • Employee:

    <前><代码>@ManyToOne @JoinColumn(name = "fk_department_id", nullable = true) 私人部门;
  • 部门:

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(名称 = "fk_department_id")
    私有集员工 = new HashSet<员工>(0);
    

任何人都可以向我提供此转换的示例吗,因为我读了一些示例,但我仍然不知道如何做到这一点。

I want to convert the following subquery to use hibernate subquery:

getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)")
                   .list();
  • Employee:

    @ManyToOne
    @JoinColumn(name = "fk_department_id", nullable = true) 
    private Department department;
    
  • Department:

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "fk_department_id")
    private Set<Employee> employees = new HashSet<Employee>(0);
    

Can anyone please provide me with an example of this convert, because i read some examples and i still cannot figure out how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2024-12-26 11:22:19
Criteria c = session.createCriteria(Employee.class, "e");
DetachedCriteria dc = DetachedCriteria.forClass(Departemt.class, "d");
dc.add(Restrictions.isNotNull("d.adminId");
dc.setProjection(Projections.property("d.adminId"));
c.add(Subqueries.propertyIn("e.id", dc));

setProjection 调用使子查询仅返回 adminId 属性,而不是整个 Department 实体。 Subqueries.propertyIn 创建了一个限制:搜索员工的属性 id 必须位于子查询返回的结果集中中。

Criteria c = session.createCriteria(Employee.class, "e");
DetachedCriteria dc = DetachedCriteria.forClass(Departemt.class, "d");
dc.add(Restrictions.isNotNull("d.adminId");
dc.setProjection(Projections.property("d.adminId"));
c.add(Subqueries.propertyIn("e.id", dc));

The setProjection call makes the subquery return the adminId property only instead of the whole Department entity. The Subqueries.propertyIn creates a restriction: the property id of the searched employee must be in the set of results returned by the subquery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文