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 ]