2
votes

I want this 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')) 

If I use alias for table2:

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

and set like this:

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

Hibernate always generates INNER JOIN in SELECT query which I don't want and that gives me wrong results.

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

How can I achive "INNER JOIN" in WHERE clause (t1.id2 = t2.id) not in SELECT?

1
This seems to be a left outer join from table1 to table2, isn't it? - oddparity

1 Answers

0
votes

Using the Criteria API, you can't. You can only query from a root entity and create aliases or subcriterias to navigate to other entities through their associations. Selecting from two entities at once is not possible with this API. It's possible using HQL, though.