I’m sure it’s doable, but it’s definitely not easy to do.
Hibernate has added a complex, sorry “rich”, API on top of JDBC to construct queries and the like (in addition to OR mapping and I’m sure a host of other tings). Now, most tutorials out there are mere Micky Mouse examples and not that useful for what I need right now: joining three (maybe four tables) and returning an entity (and yes, I do know that joining many tables is expensive).
I’ve joined two tables before using one DetachedCriteria & Criteria and I assume I then can do a three table join by using two DetachedCriterias? Maybe, have to try this later. Matter of fact is that the API to construct queries is so complex, create so long code lines and so many of them, that I ventured into doing this using Hibernate’s own SQL dialect.
Of course, I believe it’s doable but I couldn’t find any good examples out there on doing it, I got this Array (or something) of java.lang.Objects rather than my nicely mapped Hibernate entity when I executed my Query object.
Object thing=[[Ljava.lang.Object;@956b74]
I didn’t see any good way of manipulating this Object thing, so I ventured lower down the food chain to the native SQL support in Hibernate:
SQLQuery query = Session.createSQLQuery(String);
Thanks to this one and setting the return entity, with the alias I used in the SQL String:
query.addEntity("b", Blue.class);
I finally got this thing working.
Now, to all of the Hibernate enthusiasts out there; if there’s a beautiful way of joining three tables returning an entity using the high level Hibernate API, I love to hear about it. But for now, I’m happy with my Hibernate/native SQL solution