Hi,
I have found that if the mapping contains components in a bag container with fetch=join, the method list() returns a list with more than one instance of the same entity under some circumstances.
The example below contains two classes, Parent and Child. The Parent contains several Child objects. The Parent is an entity, but children are components (not entities).
Hibernate version: 3.2.6 GA
Classes:
public class Child {
private String name;
private int age;
public Child() {}
public Child(String name, int age) {this.name = name; this.age = age;}
public String getName() {return this.name;}
public void setName(String name) {this.name = name;}
public int getAge() {return this.age;}
public void setAge(int age) {this.age = age;}
public String toString() {
return "[child: name=" + this.name + ", age=" + this.age + "]";
}
}
public class Parent {
private Long id;
private String name;
private List<Child> children = new ArrayList<Child>();
public Parent() {}
public Parent(String name) {this.name = name;}
public Long getId() {return this.id;}
public void setId(Long id) {this.id = id;}
public String getName() {return this.name;}
public void setName(String name) {this.name = name;}
public List<Child> getChildren() {return this.children;}
public String toString() {
return "[parent: id=" + this.id + ", name=" + this.name + ", children=" + this.children + "]";
}
}
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Parent">
<id name="id" unsaved-value="null" >
<generator class="identity" />
</id>
<property name="name" type="string" />
<bag name="children" fetch="join" access="org.hibernate.property.DirectPropertyAccessor" >
<key column="id" />
<composite-element class="Child">
<property name="name" type="string" />
<property name="age" type="int" />
</composite-element>
</bag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
session.beginTransaction();
// Create 2 Parents.
Parent p_1 = new Parent("p_1");
p_1.getChildren().add(new Child("c_1.1", 10));
Parent p_2 = new Parent("p_2");
p_2.getChildren().add(new Child("c_2.1", 1));
p_2.getChildren().add(new Child("c_2.2", 2));
p_2.getChildren().add(new Child("c_2.3", 3));
session.saveOrUpdate(p_1);
session.saveOrUpdate(p_2);
session.getTransaction().commit();
// Delete 1 Parent.
session.beginTransaction();
session.delete(p_1);
session.getTransaction().commit();
// List all Parents and print them.
session.beginTransaction();
List result = session.createCriteria(Parent.class).list();
for (int i = 0; i < result.size(); i++)
{
System.out.println(result.get(i));
}
session.getTransaction().commit();
Name and version of the database you are using:
HSQLDB 1.8.0.1
The generated SQL (show_sql=true):
select this_.id as id0_0_, this_.name as name0_0_, children2_.id as id2_, children2_.name as name2_, children2_.age as age2_ from Parent this_ left outer join children children2_ on this_.id=children2_.id
The generated SQL looks fine, but the method list() returns a list with 3 elements. Each element is p_2 (the second Parent object). If the mapping is changed to fetch="select" (the default), the list contains just one Parent object as expected. The error happens only if one Parent is deleted before executing the method list().
|