This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes So much on the Left Joins in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "So much on the Left Joins in Hibernate" Watch "So much on the Left Joins in Hibernate" New topic

So much on the Left Joins in Hibernate

Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
Part I:
I recently adopted Hibernate and am still confused when it comes to creating the mapping files. Whether to include sets of children in parent table objects and one-to-many relationship definitions in the parent table mapping file or define parent table entries in child objects with many-to-one relationship defined in the child table mapping file or adopt both ways as per the requirements.

I can either write HQL left join queries either as

in the first case as we mention kittens as a set in the object file and the relationship between cat and kittens as one-to-many from cat to kittens.

In the second case, which I have always been interested in

in the second case, which seems to be not possible for some reason. So does this mean we cannot left join un-related tables or rather when no mapping is defined in the mapping file to define a relationship?

I am not sure if the code has correct syntax in a left join of a parent table object with its child object though.

Part II
I have been writing HQL queries in hibernate to get all the tables' columns' data in one single query just like any other JDBC query. Writing LEFT JOINS on many tables was harder and I was not sure if I was doing it right anyways. Could any body suggest a better way to modify the object and mapping file to LEFT JOIN many tables please...

Lets say TableA->TableB->TableC<-TableD and TableA->TableE->TableF where -> indicates the parent to child relationships and I have to get data from all of these tables in one shot. I created a new object with fields and constructors to populate only those fields which are required. I see that the LEFT JOIN query is a little complicated as, I had to include TableB objects as a set in Table A and TableC objects as a set in TableB with a
and change the TableAObj mapping file to include TableB set as

However, things change when relating TableC and TableD this way. So I had to write them differently as

TableCObj has TableDObj field
and the TableCObj mapping file had to be written as:

And finally get all the populated objects in a single query as:

Is this the optimal way of creating objects whose fields populate different data from different columns of different tables?

Also is it ok to set any of the getter methods in these object files as follows:

Similary is it correct to include more logic in the setters method, say some field setter method that sets based on other fields as in

assuming they are already set by their setter methods??

[ March 25, 2008: Message edited by: Rama Krishna ]

[ March 25, 2008: Message edited by: Rama Krishna ]
[ March 25, 2008: Message edited by: Rama Krishna ]
I agree. Here's the link:
subject: So much on the Left Joins in Hibernate
jQuery in Action, 3rd edition