-->
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.  [ 3 posts ] 
Author Message
 Post subject: Why hibernate recreates all list (mapped as <bag>) eac
PostPosted: Sun Sep 19, 2004 5:42 pm 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
I am using a List field in my domain model class. It represents many-to many association. It is umidirectional association betwen Class1 and Class2. In mapping I am using <bag> for this list.

The QUESTION is: Why hibernate recreates list (mapped as <bag> reprezenting many-to-many association) each time I simply add new element to it?

Hibernate version:

2.1.6

Domain model classes:

package jbeans;

public class Class2 {

private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

}

package jbeans;

import java.util.*;

public class Class1 {

private Long id;

private List classes2;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List getClasses2() {
return classes2;
}

public void setClasses2(List classes2) {
this.classes2 = classes2;
}

public void addClass2(Class2 class2){
classes2.add(class2);
}
}

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="jbeans">
<class
name="Class1"
table="CLASSES1">
<id
name="id"
type="long"
column="ID"
>
<generator class="native"/>
</id>
<bag
name="classes2"
lazy="true"
table="CLASSES1_CLASESS2"
>
<key column="CLASS1_ID"/>
<many-to-many class="Class2" column="CLASS2_ID"/>
</bag>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="jbeans">
<class
name="Class2"
table="CLASSES2">
<id
name="id"
type="long"
column="ID"
>
<generator class="native"/>
</id>
</class>
</hibernate-mapping>

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

/*Insert new Class1*/
/*This is executed only once after database creation*/
tr = ses.beginTransaction();
Class1 c11 = new Class1();
ses.save(c11);
tr.commit();
ses.close();

/*Insert new Class2 and associate with Class1*/
/*This is executed few times after new Class1 insert in new Hibernate session*/
tr = ses.beginTransaction();
Criteria criteria = ses.createCriteria(Class1.class);
List result = criteria.list();
Class1 c12 = (Class1)result.get(0);
Class2 c21 = new Class2();
ses.save(c21);
c12.getClasses2().add(c21);
tr.commit();

Name and version of the database I am using:
MySQL 4.0.13

The generated SQL (show_sql=true):

/*When Insert new Class1 is executed*/
Hibernate: insert into CLASSES1 values ( )

/*When Insert new Class2 and associate with Class1 is execited*/

/*First time*/
Hibernate: select this.ID as ID0_ from CLASSES1 this where 1=1
Hibernate: insert into CLASSES2 values ( )
Hibernate: select classes20_.CLASS1_ID as CLASS1_ID__, classes20_.CLASS2_ID as CLASS2_ID__, class21_.ID as ID0_ from CLASSES1_CLASESS2 classes20_ inner join CLASSES2 class21_ on classes20_.CLASS2_ID=class21_.ID where classes20_.CLASS1_ID=?
Hibernate: insert into CLASSES1_CLASESS2 (CLASS1_ID, CLASS2_ID) values (?, ?)


/*Second time*/
Hibernate: select this.ID as ID0_ from CLASSES1 this where 1=1
Hibernate: insert into CLASSES2 values ( )
Hibernate: select classes20_.CLASS1_ID as CLASS1_ID__, classes20_.CLASS2_ID as CLASS2_ID__, class21_.ID as ID0_ from CLASSES1_CLASESS2 classes20_ inner join CLASSES2 class21_ on classes20_.CLASS2_ID=class21_.ID where classes20_.CLASS1_ID=?
Hibernate: delete from CLASSES1_CLASESS2 where CLASS1_ID=?
Hibernate: insert into CLASSES1_CLASESS2 (CLASS1_ID, CLASS2_ID) values (?, ?)
Hibernate: insert into CLASSES1_CLASESS2 (CLASS1_ID, CLASS2_ID) values (?, ?)

/*Third time*/
Hibernate: select this.ID as ID0_ from CLASSES1 this where 1=1
Hibernate: insert into CLASSES2 values ( )
Hibernate: select classes20_.CLASS1_ID as CLASS1_ID__, classes20_.CLASS2_ID as CLASS2_ID__, class21_.ID as ID0_ from CLASSES1_CLASESS2 classes20_ inner join CLASSES2 class21_ on classes20_.CLASS2_ID=class21_.ID where classes20_.CLASS1_ID=?
Hibernate: delete from CLASSES1_CLASESS2 where CLASS1_ID=?
Hibernate: insert into CLASSES1_CLASESS2 (CLASS1_ID, CLASS2_ID) values (?, ?)
Hibernate: insert into CLASSES1_CLASESS2 (CLASS1_ID, CLASS2_ID) values (?, ?)
Hibernate: insert into CLASSES1_CLASESS2 (CLASS1_ID, CLASS2_ID) values (?, ?)

Etc.

_________________
Lmichasz


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 5:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the Hibernate reference documentations. (The section on collection performance.)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 11:37 pm 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
OK.
So I tried to use <idbag> instead of <bag>. It works much better. One problem is that when I want to insert new element a full list is loaded from database. Besides it creates surrogate key... I have read that a solution of this problem is mapping this association as two bidirectional one-to-many associations with additional association class and declare this collections as (in my case inverse <bag>). Is it the only sollution of this problem?

_________________
Lmichasz


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

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.