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
Code:
<set name="TableBObjects"
inverse="true"
lazy="false"
cascade="all">
<key column="TableAPrimaryKey"/>
<one-to-many class="TableB"/>
</set>
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:
Code:
<many-to-one name="TableDObj" column="TableDPrimatyKey" unique="true"/>
And finally get all the populated objects in a single query as:
Code:
Select new objectConst(AliasObjTableA.Field1, AliasObjTableB.Field2, AliasObjOfTableC.Field3, AliasObjTablD.Field4, AliasObjTableE.Field5,...)
from ObjTableA as AliasObjTableA
LEFT JOIN ObjTableA.TableBObjects as AliasObjTableB
LEFT JOIN AliasObjTableB.TableCObjects as AliasObjTableC
LEFT JOIN AliasObjTableC.TableDObj as AliasObjTableC
LEFT JOIN ObjTableA.TableDObjects as AliasObjTableD
where....
Is this the correct 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:
Code:
...
public void setProperty(String somePropValue)
{
if (somePropValue == null)
this.setProperty(" ");
else
this.setProperty(somePropValue):
}
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
Code:
...
public void setProperty(String somePropValue)
{
this.setProperty(this.getSomeProp1 + this.getSomeProp2):
}
assuming they are already set by their setter methods??