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: 790This 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>