Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3+
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.citigroup.regtax.calendar.pojo">
<class name="Project" table="Project">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="programNumber" column="programNumber"/>
<property name="programName" column="programName"/>
<property name="workNumber" column="workNumber"/>
<set name="applications" inverse="true" lazy="false">
<key column="projectId"/>
<one-to-many class="Application"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.citigroup.regtax.calendar.pojo">
<class name="Application" table="Application">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
not-null="true"/>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
MySQL 5.0
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
The domain model looks like this: A project has 0, 1 or more applications.
I would like to retrieve a projects with its applications so that the applicions are stored in the project's applications attribute. If I use the following HQL:
Collection projects = this.getHibernateTemplate().find(
"select p from Project p order by p.name");
This works find. All projects are being retrieved and each project's applictions atribute (Collection) contains the associated applictions. I am just not happy that it doesn't do a join, but it does separate selects for each project to find it's associated applictions:
Hibernate: select this_.id as id7_0_, this_.name as name7_0_, this_.programNumber as programN3_7_0_, this_.programName as programN4_7_0_, this_.workNumber as workNumber7_0_, this_.projectManagerId as projectM6_7_0_, this_.programManagerId as programM7_7_0_ from calendar.Project this_ order by this_.name asc
Hibernate: select applicatio0_.projectId as projectId1_, applicatio0_.id as id1_, applicatio0_.id as id0_0_, applicatio0_.name as name0_0_, applicatio0_.projectId as projectId0_0_ from calendar.Application applicatio0_ where applicatio0_.projectId=?
Hibernate: select applicatio0_.projectId as projectId1_, applicatio0_.id as id1_, applicatio0_.id as id0_0_, applicatio0_.name as name0_0_, applicatio0_.projectId as projectId0_0_ from calendar.Application applicatio0_ where applicatio0_.projectId=?
Hibernate: select applicatio0_.projectId as projectId1_, applicatio0_.id as id1_, applicatio0_.id as id0_0_, applicatio0_.name as name0_0_, applicatio0_.projectId as projectId0_0_ from calendar.Application applicatio0_ where applicatio0_.projectId=?
I tried forcing outer joining:
<set name="applications" inverse="true" lazy="false" fetch="join">
<key column="projectId"/>
<one-to-many class="Application"/>
</set>
With the following contents of the tables appliction and project:
Project
project_id project_name
1 project 1
2 project 2
Application
application_id application_name project_id
1 application 1 1
2 application 2 1
3 application 3 2
4 application 4 2
The correct sql is being generated:
select this_.id as id7_1_, this_.name as name7_1_, this_.programNumber as programN3_7_1_, this_.programName as programN4_7_1_, this_.workNumber as workNumber7_1_, this_.projectManagerId as projectM6_7_1_, this_.programManagerId as programM7_7_1_, applicatio2_.projectId as projectId3_, applicatio2_.id as id3_, applicatio2_.id as id0_0_, applicatio2_.name as name0_0_, applicatio2_.projectId as projectId0_0_ from calendar.Project this_ left outer join calendar.Application applicatio2_ on this_.id=applicatio2_.projectId order by this_.name asc
But the mapping of the result set to the project and its applictions doesn't work correctly because it retrieves 4 projects with each 2 applications, and not 2 projects with each 2 applications.
Anybody knows how to avoid the extra selects and just make hibernate generate a join in order to retrieve objects with it's childs (one-to-many relationships) ?
Greetz