Hibernate version:
2.1.7
Mapping documents:
<class name="com.argus.issueTracker.domain.vo.Project" table="projects">
<id name="projectId" column="project_id" type="integer">
<generator class="increment"/>
</id>
<property name="projectName" column="project_name" type="string"/>
<property name="creationDate" column="creation_date" type="timestamp"/>
<property name="createdBy" column="created_by" type="integer"/>
<property name="status" column="status" type="string"/>
<property name="parentProjectId" column="parent_id" type="integer"/>
<bag name="threads" order-by="opened desc" outer-join="true" cascade="delete" inverse="true">
<key column="project_id"/>
<one-to-many class="com.argus.issueTracker.domain.vo.Thread"/>
</bag>
<many-to-one name="parentProject" insert="false" update="false" class="com.argus.issueTracker.domain.vo.Project" outer-join="true" column="parent_id" cascade="none" />
<bag name="childProjects" order-by="project_id asc" outer-join="true" cascade="delete" inverse="true" >
<key column="parent_id"/>
<one-to-many class="com.argus.issueTracker.domain.vo.Project"/>
</bag>
</class>
Here's the problem.
In the above mapping file, I have 2 one-to-many collections that I am mapping. 1 is the child projects of this project, and another is the threads that this project contains (this is for a issue tracking app).
When I execute the following query
Code:
Criteria criteria = session.createCriteria(Project.class);
criteria.addOrder(Order.desc("creationDate"));
List projects = criteria.list();
DB entries look like
Code:
Projects Table
project_id project_name parent_id
1 Base Project null
2 Test Project null
3 Base Sub 1 1
4 Base Sub 2 1
5 Test Sub 1 2
6 Test Sub 2 2
7 Test Sub 3 2
8 Base Sub 2 1
Threads Table
thread_id subject project_id
1 Test 1
2 Test1 1
3 Test 2
4 Test1 2
So what I expected to get back (beings I didn't put any where clause) was 8 entries.
Entries with ID 1 and 2 should each have a collection with 3 childProjects in it.
Each should also have a collection with 2 threads in it.
Instead what I get back is 10 entries. Projects 3-6 return as a single line, but projects 1 and 2 are duplicated becuase they each have 2 threads. It isn't duplicating the Projects based off fo the sub project size (becuase if it was it would have generated more than 10 results.
I am not posting the query becuase it would just fill the post up. But basically when it is building the mapped collection of childProjects it does so by issuing separate SQL statements (which help prevent duplication of the parent). For the threads, it tries to get them back in the same query as the projects, which results in duplicated rows.
Why is Hibernate giving me back duplicate entries for the "Projects" when there are multiple children?
What do I need to do to fix it?
Where can I find better documentation on this?
I have spent 2 days reading through the forum and the documentation, and nothing I have found addresses this. Also, the forum seriously needs a better search capability. A query either returns 2000+ rows or none........ But that's just my frustration coming out.