首页 文章

Hibernate子查询将DetachedCriteria应用于ManyToMany连接表

提问于
浏览
1

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

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

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

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

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

1 回答

  • 2

    我放弃了尝试使用DetachedCriteria并在条件上使用了sqlRestriction;

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

相关问题