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
|