I did a search through the forum on this issue finding at least half a dozen posts, but with no satisfactory explanation.
Simplified info to my specific project:
I am working on a Coldfusion project which uses hibernate 3.5.2-Final
I have a table dbo.resource which has a one-to-many relationship to dbo.resourceDetail
The problem seems to be that the Criteria Query does not support inner join whenever you want to fetch eagerly. You can do something like this:
Code:
Criteria c = sessionFactory.getCurrentSession().createCriteria(Resource);
c.createAlias("ResourceDetail", "rd", c.INNER_JOIN);
c.list();
And this produces SQL with a single select and an inner join just like you would think. The problem is that is fetches lazily, so whenever you want to actually access resourceDetail, you get the N+1 selects problem. The solution is:
Code:
c.setFetchMode("ResourceDetail", FetchMode.JOIN);
c.createAlias("ResourceDetail", "rd", c.INNER_JOIN);
c.list();
But this forces an outer join. I need to do an inner join. I need to fetch all the data at once. And I'd much rather use a Criteria Query.
Using HQL is an obvious solution to this problem, but I like Criteria Query much better, because it looks nicer and its more programmatic. So it appears the Criteria Query was simply written without the ability to fetch with inner joins (which seems crazy to me), but are there any work arounds that any knows about? Any assistance would be greatly appreciated.