-->
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.  [ 2 posts ] 
Author Message
 Post subject: Wrong SQL generated when using many-to-one associations
PostPosted: Tue Dec 11, 2007 8:39 am 
Newbie

Joined: Tue Dec 11, 2007 8:02 am
Posts: 1
Hi:

I am having troubles (and a horrible headache) with several associations in my code.

My system
Hibernate version: 3.3
MySQL 5/ Oracle 10i

The associations are all "many-to-one" and defined like this:

Code:
  <class name="Tmaster" table="tmaster">
      <id name="id" type="long" column="TMASTER_ID" >
      <generator class="assigned"/>
     </id>

     <property name="name">
       <column name="TMASTER_NAME" />
     </property>
     <property name="lastName">
      <column name="TMASTER_LASTNAME"/>
     </property>
     <property name="email">
      <column name="TMASTER_EMAIL"/>
     </property>
     <!-- property name="insurance">
      <column name="INSURANCE"/>
     </property-->
      <many-to-one name="related"
       column="RELATED_ID"
       class="Related"
       not-null="false"
       insert="true"
       update="true"
       fetch="join"/>    
    
   </class>


In EVERY case the generated SQL was:

Code:
select tmaster0_.TMASTER_ID as col_0_0_,
tmaster0_.TMASTER_NAME as col_1_0_,
tmaster0_.TMASTER_LASTNAME as col_2_0_,
tmaster0_.TMASTER_EMAIL as col_3_0_,
related1_.RELATED_NAME as col_4_0_
from tmaster tmaster0_,
related related1_
where tmaster0_.RELATED_ID=related1_.RELATED_ID


The SELECT I use in my code is something like:

Code:
             String SQL_QUERY ="select _t.id,
                                                              _t.name,
                                                              _t.lastName,
                                                              _t.email,
                                                              _t.related.name
                                                            from Tmaster _t";
             Query query = session.createQuery(SQL_QUERY);

             List <Object>resultado = query.list();




The problem: I need a JOIN into the SQL and not a WHERE because if I have null values in the related column the values won't be returned.

I tried all the many-to-one attributes with no results, even tried a one-to-many association in the "other side" and always there is a damn WHERE.

Any idea?

Greetings


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 9:32 am 
Newbie

Joined: Mon Oct 29, 2007 2:33 pm
Posts: 16
Location: Cambridge, MA
This generated SQL *is* a join, and works the same as a plain JOIN, i.e., an inner join, dropping results for null FKs in the main table. It sounds like you want an outer join, in which case you need to keywords in the HQL query to get that, such as:
String SQL_QUERY ="select _t.id,
_t.name,
_t.lastName,
_t.email,
left join _t.related.name
from Tmaster _t";

or "left join fetch" if you want eager fetching.


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

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.