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.  [ 1 post ] 
Author Message
 Post subject: Child set collection filled incorrectly
PostPosted: Tue Feb 12, 2008 11:54 pm 
Newbie

Joined: Tue Feb 12, 2008 11:36 pm
Posts: 1
I can't seem to figure why this is happening. I've mocked up a scenario to illustrate the problem. I have a Employee class which contains a set of dependent classes. When I do HQL to get all the Employees, the dependents for each employee is either the first or last employees dependents, depending on weather I use default-lazy="false" or not. Can anybody help?

Here is the database:

---
--- Tables
---

DROP TABLE IF EXISTS `Employee`;
CREATE TABLE `Employee`
(
`EmpID` int(11) NOT NULL auto_increment,
`Name` TEXT NOT NULL,
PRIMARY KEY (`EmpID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `Dependent`;
CREATE TABLE `Dependent`
(
`EmpID` int(11) NOT NULL,
`DepID` int(11) NOT NULL auto_increment,
`Name` TEXT NOT NULL,
PRIMARY KEY (`EmpID`,`DepID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

---
--- Employees
---

INSERT INTO `Employee` VALUES( 1, 'John Doe' );
INSERT INTO `Dependent` (EmpID, Name) VALUES( 1, 'John Doe Jr.' );
INSERT INTO `Dependent` (EmpID, Name) VALUES( 1, 'Amy Doe' );
INSERT INTO `Dependent` (EmpID, Name) VALUES( 1, 'Jill Doe' );
INSERT INTO `Dependent` (EmpID, Name) VALUES( 1, 'Karl Doe' );

INSERT INTO `Employee` VALUES( 2, 'Daniel Smith' );
INSERT INTO `Dependent` (EmpID, Name) VALUES( 2, 'Allan Smith' );
INSERT INTO `Dependent` (EmpID, Name) VALUES( 2, 'Eric Smith' );

And here is the Employee.java and Dependent.java classes:


package HibCollectionTest;

import java.util.*;

public class Employee
{
private String name;
private int empID;
private Set<Dependent> dependents;

public String getName() { return name; }
public void setName( String name ) { this.name = name; }

public int getEmpID() { return empID; }
public void setEmpID( int empID ) { this.empID = empID; }

public Set<Dependent> getDependents() { return dependents; }
public void setDependents( Set<Dependent> dependents )
{
this.dependents = dependents;
}
}

package HibCollectionTest;

import java.util.*;

public class Dependent
{
private String name;
private int depID;

public String getName() { return name; }
public void setName( String name ) { this.name = name; }

public int getDepID() { return depID; }
public void setDepID( int depID ) { this.depID = depID; }
}


Hibernate version:

3

Mapping documents:


Code:
<?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 default-lazy="true">

    <class name="HibCollectionTest.Employee" table="employee">
        <id name="empID" type="int" column="EmpID">
            <generator class="native"/>
        </id>

        <property name="name" type="string" column="Name"/>

        <set name="dependents" order-by="depID" cascade="all">
            <key column="empID"/>
            <one-to-many class="HibCollectionTest.Dependent"/>
        </set>

    </class>

    <class name="HibCollectionTest.Dependent" table="dependent">
        <id name="depID" type="int" column="DepID">
            <generator class="native"/>
        </id>

        <property name="name" type="string" column="Name" />

    </class>

</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

String hqlString = "from Employee";
Query query = session.createQuery( hqlString );
List results = query.list();

for ( int i = 0; i < results.size(); i++ )
{
printEmployee( (Employee) results.get( i ) );
}



Full stack trace of any exception that occurs:

Name and version of the database you are using:

MySql 5

The generated SQL (show_sql=true):

Lazy:

[java] Hibernate: select employee0_.EmpID as EmpID0_, employee0_.Name as Na
me0_ from employee employee0_
[java] EMP: 1 (John Doe)
[java] DEPS:
[java] Hibernate: select dependents0_.empID as empID1_, dependents0_.DepID
as DepID1_, dependents0_.DepID as DepID1_0_, dependents0_.Name as Name1_0_ from
dependent dependents0_ where dependents0_.empID=? order by dependents0_.depID
[java] 1 (John Doe Jr.)
[java] 2 (Amy Doe)
[java] 3 (Jill Doe)
[java] 4 (Karl Doe)
[java] EMP: 2 (Daniel Smith)
[java] DEPS:
[java] Hibernate: select dependents0_.empID as empID1_, dependents0_.DepID
as DepID1_, dependents0_.DepID as DepID1_0_, dependents0_.Name as Name1_0_ from
dependent dependents0_ where dependents0_.empID=? order by dependents0_.depID
[java] 1 (John Doe Jr.)
[java] 2 (Amy Doe)


Default-Lazy = False:

[java] Hibernate: select employee0_.EmpID as EmpID0_, employee0_.Name as Na
me0_ from employee employee0_
[java] Hibernate: select dependents0_.empID as empID1_, dependents0_.DepID
as DepID1_, dependents0_.DepID as DepID1_0_, dependents0_.Name as Name1_0_ from
dependent dependents0_ where dependents0_.empID=? order by dependents0_.depID
[java] Hibernate: select dependents0_.empID as empID1_, dependents0_.DepID
as DepID1_, dependents0_.DepID as DepID1_0_, dependents0_.Name as Name1_0_ from
dependent dependents0_ where dependents0_.empID=? order by dependents0_.depID
[java] EMP: 1 (John Doe)
[java] DEPS:
[java] 1 (Allan Smith)
[java] 2 (Eric Smith)
[java] 3 (Jill Doe)
[java] 4 (Karl Doe)
[java] EMP: 2 (Daniel Smith)
[java] DEPS:
[java] 1 (Allan Smith)
[java] 2 (Eric Smith)


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

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.