-->
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: list() returns more than one instance of entity
PostPosted: Fri Jul 25, 2008 8:50 am 
Newbie

Joined: Fri May 04, 2007 2:50 am
Posts: 1
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().


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.