【问题标题】:Hibernate subquery apply DetachedCriteria to ManyToMany join tableHibernate 子查询将 DetachedCriteria 应用于 ManyToMany 连接表
【发布时间】:2015-07-03 13:34:41
【问题描述】:

我正在尝试创建一个子查询,该子查询将采用 DetachedCriteria,它从存在实体条目的连接表中进行选择;本质上我想通过连接表查看一个实体是否有任何关联。

根据传递的布尔值,我将在条件中添加 existsnotExists 子查询。

基本上我想在这个sql中取子查询:

select * from A as leftEntity where not exists (select * from A_B where A = leftEntity.id);`

并创建一个分离的标准。我无法确定要传递的正确实体来创建分离条件 - 我正在尝试在连接表上做某事,而不是实体 A 或 B。我该怎么做?

【问题讨论】:

    标签: java hibernate subquery detachedcriteria


    【解决方案1】:

    我放弃了尝试使用 DetachedCriteria,只是在条件上使用了 sqlRestriction;

    criteria.add(Restrictions.sqlRestriction("exists (select a from a_b where a = {alias}.id)"));

    【讨论】:

    • 我喜欢你的方法 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2016-08-17
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多