首页 文章

内部加入标准Hibernate

提问于
浏览
2

我想要这个sql:

SELECT COUNT(*) FROM table1 t1, table2 t2 
WHERE t1.id > 0  
AND (( t1.name = 'foo') 
    OR ( t1.id2 = t2.id AND t1.name = 'det'))

如果我使用table2的别名:

detachedCriteria.createAlias("table2", "t2");

并设置如下:

detachedCriteria.add(Restrictions.eqProperty("t1.id2", "t2.id"));

Hibernate总是在SELECT查询中生成INNER JOIN,这是我不想要的,这给了我错误的结果 .

select count(*) as y0_ 
from
    table1 this_ 
inner join
    table2 table2_ 
        on this_.id2=table2_.id 
where 
...

如何在WHERE子句(t1.id2 = t2.id)中不在SELECT中执行“INNER JOIN”?

1 回答

  • 0

    使用Criteria API,您不能 . 您只能从根实体进行查询,并创建别名或子标准以通过其关联导航到其他实体 . 使用此API无法一次从两个实体中进行选择 . 但是,可以使用HQL .

相关问题