-->
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.  [ 7 posts ] 
Author Message
 Post subject: Fetch Join Query
PostPosted: Fri Aug 20, 2010 5:22 pm 
Newbie

Joined: Sat Jul 17, 2010 3:43 pm
Posts: 8
Hi, I'm developing a web application with Struts 2, Spring, Hibernate, Tomcat and Postgres, I need to query a Database that has countries, states and cities that are related, to load a triple select with them, I'm trying this query:
Code:
Query query = session.createQuery("from Country c left join fetch c.states s left join fetch s.cities");


and I get the following error...


WARNING: SQL Error: 0, SQLState: 42703
Aug 20, 2010 6:10:58 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ERROR: column states1_.country_id does not exist
Position: 790


This is the query, generated by Hibernate...

Code:
select country0_.country_id as country1_24_0_, states1_.state_id as state1_22_1_, cities2_.city_id as city1_23_2_, country0_.country_object_version as country2_24_0_, country0_.country_created as country3_24_0_, country0_.country_name as country4_24_0_, states1_.state_object_version as state2_22_1_, states1_.state_created as state3_22_1_, states1_.state_name as state4_22_1_, states1_.state_country_id as state5_22_1_, states1_.country_id as country6_0__, states1_.state_id as state1_0__, cities2_.city_object_version as city2_23_2_, cities2_.city_created as city3_23_2_, cities2_.city_name as city4_23_2_, cities2_.city_state_id as city5_23_2_, cities2_.state_id as state6_1__, cities2_.city_id as city1_1__ from country country0_ left outer join state states1_ on country0_.country_id=states1_.country_id left outer join city cities2_ on states1_.state_id=cities2_.state_id


These are the hbm.xml files...

Country.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping package="ro.model" default-access="property">

<class name="Country" table="country" batch-size="20" >

   <id name="id" type="long" column="country_id">
      <generator class="identity"/>
   </id>

   <version name="version" column="country_object_version" />
   <property name="created" column="country_created" type="timestamp" update="false" not-null="true"/>
   
   <property name="name" type="string" column="country_name" length="256" not-null="true" />
   
   <set name="addresses" inverse="true">
      <key column="country_id" foreign-key="fk_country_address" />
      <one-to-many class="Address" />
   </set>
   
   <set name="states" inverse="true" lazy="false" fetch="join">
      <key column="country_id" foreign-key="fk_country_state"/>
      <one-to-many class="State" />
   </set>
      
</class>
   
</hibernate-mapping>



State.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping package="ro.model" default-access="property">

<class name="State" table="state" batch-size="20">

   <id name="id" type="long" column="state_id">
      <generator class="identity"/>
   </id>

   <version name="version" column="state_object_version" />
   <property name="created" column="state_created" type="timestamp" update="false" not-null="true"/>
   
   <property name="name" type="string" column="state_name" length="256" not-null="true" />
   
   <set name="addresses" inverse="true">
      <key column="state_id" foreign-key="fk_state_address" />
      <one-to-many class="Address" />
   </set>
   
   <set name="cities" lazy="false" inverse="true" fetch="join">
      <key column="state_id" foreign-key="fk_state_city"/>
      <one-to-many class="City" />
   </set>
   
   <many-to-one name="country" class="Country" column="state_country_id" foreign-key="fk_state_country" not-null="false" />
      
</class>
   
</hibernate-mapping>


City.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping package="ro.model" default-access="property">

<class name="City" table="city" batch-size="20">

   <id name="id" type="long" column="city_id">
      <generator class="identity"/>
   </id>

   <version name="version" column="city_object_version" />
   <property name="created" column="city_created" type="timestamp" update="false" not-null="true"/>
   
   <property name="name" type="string" column="city_name" length="256" not-null="true" />
   <many-to-one name="state" class="State" column="city_state_id" foreign-key="fk_city_state" not-null="false" />
   
   <set name="addresses" inverse="true">
      <key column="city_id" foreign-key="fk_city_address" />
      <one-to-many class="Address" />
   </set>
         
</class>
   
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Fetch Join Query
PostPosted: Fri Aug 20, 2010 6:57 pm 
Newbie

Joined: Sat Jul 17, 2010 3:43 pm
Posts: 8
I have set lazy=true in every hbm.xml, it's important to say, that without the query with every hbm.xml with lazy=false, the same query error is thrown...


Top
 Profile  
 
 Post subject: Re: Fetch Join Query
PostPosted: Sat Aug 21, 2010 8:47 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The column names in the bi-directional mappings doesn't match. In State.hbm.xml you have:

Code:
<many-to-one name="country" class="Country" column="state_country_id" foreign-key="fk_state_country" not-null="false" />


and in Country.hbm.xml you have:
Code:
   <set name="states" inverse="true" lazy="false" fetch="join">
      <key column="country_id" foreign-key="fk_country_state"/>
      <one-to-many class="State" />
   </set>


The column name inside the <key> tag should match the column name inside the <many-to-one> tag.

There seems to be a similar issue in the association between City and State.


Top
 Profile  
 
 Post subject: Re: Fetch Join Query
PostPosted: Mon Aug 23, 2010 5:47 pm 
Newbie

Joined: Sat Jul 17, 2010 3:43 pm
Posts: 8
Thank you, it's obvious and too simple, yet I didn't notice.
The many to one relationship and the set in this case, to which column in which table do they refer? I thought that many to one mapping should point to the corresponding column in the Entity State, and that the set should point to the corresponding column in the Country entity... When I say corresponding column, I mean the column that is affected by the relationship, I think I inferred what I am saying by the way I would have related the tables in the database... What I am misunderstanding?

Thank you.


Top
 Profile  
 
 Post subject: Re: Fetch Join Query
PostPosted: Mon Aug 23, 2010 6:12 pm 
Newbie

Joined: Sat Jul 17, 2010 3:43 pm
Posts: 8
What about the foreign key, should it be the same one?


Top
 Profile  
 
 Post subject: Re: Fetch Join Query
PostPosted: Tue Aug 24, 2010 2:54 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The documentation (http://docs.jboss.org/hibernate/stable/ ... ration-key) doesn't mention anything about a foreign-key attribute for <key> tags... In any case, there is no need to create two identical foreign keys on the same column, so I would skip this on all inverse collections.


Top
 Profile  
 
 Post subject: Re: Fetch Join Query
PostPosted: Wed Aug 25, 2010 4:08 pm 
Newbie

Joined: Sat Jul 17, 2010 3:43 pm
Posts: 8
I have solved the problem, the query works, the problem now is that it returns repeted countries, I have read in JPA with Hibernate book that I could have written a query like this:

Code:
Query query = session.createQuery("distinct c from Country c left join fetch c.states s left join fetch s.cities");


In this case the distinct keyword acts at a List level, but it doesn't work it throws an exception... Any idea?

Maybe wrapping the result in a set... But I don't like much this idea...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.