-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Syntax help on LEFT JOIN queries in HQL, query optimization
PostPosted: Mon Mar 10, 2008 1:28 am 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
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??


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.