views:

303

answers:

2

I need to be able to do the following:

Select * from Table1 left join Table2 on id1 = id2 AND i1 = ?

Hibernate criteria doesn't allow be to specify the i1 = ? part.

The existing code is using hibernate criteria and it would be a huge refactor to swap out for HQL

Does anybody have any tips how I could implement this differently or any way to override the Hibernate Criteria? I'm not opposed to cracking open hibernate and modifying, but when I began to dig it, there seems to be layers upon layers of abstractions. I never found the the point where SQL is actually generated...

A: 

You can use a Fetch mode to Join. You can do it by doing this:

Criteria criteria = session.createCriteria(Table1.class);
criteria.setFetchMode(table2OutputList, FetchMode, JOIN);
criteria.add(Restrictions.eq("i1", i1Value);
return criteria.list();

In this case, you will have to fetch the table2OutputList before running this query. This article can give you a better idea;

Kartik
Wouldn't that just create this:SELECT * from Table1 left join Table2 on id1 = id2WHERE i1 = ?I don't want i1 = ? in my where on want it as part of the "ON" expression..
Douglas Ferguson
+1  A: 

This appears to be what I need...

http://opensource.atlassian.com/projects/hibernate/browse/HHH-2308

i.e.

public Criteria createAlias(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;

public Criteria createCriteria(String associationPath, String alias, int joinType, Criterion withClause) throws HibernateException;
Douglas Ferguson