I have the following problem with hibernate: When I make a select statement on a table which has a M-to-N directive to another table, hibernate generates a delete and insert statment.
This problem only happens if in the link-table in which are the foreign keys from the two tables have a third column. The third column (port) is as property in my hibernate mapping file defined.
I also use an ID-table for the composite key where are the equals,... functions overwritten. The delete and insert sql-statement appears only if the port value is null. I want to get all Table2 entrys and the port-value from the Table1 side, this means i have a list of Table1-Beans and iterate through this list and use Hibernate.initialize(); to initialize the binding to the link table. So it is possible to get all the values (Table1, table2 and the port value) but I olso get an insert an delete statement. What is wrong in my mapping files??
Hibernate version: 3.x (26.04.2006)
Mapping documents:Table1.hbm.xml, Table2.hbm.xml, LinkTable1Table2.hbm.xml, LinkTable1Table2Id.hbm.xml
Table1.hbm.xml looks like this:
<class name="xx.Table1" table="TABLE1">
<id name="id" type="int">
<column name="ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" not-null="true" unique="true" />
</property>
<set name="linkTable1Table2" table="LINK_TABLE1_TABLE2" lazy="true">
<key column="TABLE1_FK"/>
<composite-element class="xx.LinkTest1Test2">
<parent name="test1"/>
<many-to-one name="test2" class="xx.Test2" column="TABLE2_FK"/>
<property name="port" column="PORT"/>
</composite-element>
</set>
</class>
Table2.hbm.xml looks like this:
<class name="xx.Table2" table="TABLE2">
<id name="id" type="int">
<column name="ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" not-null="true" unique="true" />
</property>
<set name="linkTable1Table2" table="LINK_TABLE1_TABLE2" lazy="true">
<key column="TABLE2_FK"/>
<composite-element class="xx.LinkTest1Test2">
<parent name="test2"/>
<many-to-one name="test1" class="xx.Test1" column="TABLE1_FK"/>
<property name="port" column="PORT"/>
</composite-element>
</set>
</class>
and the LinkTable1Table2.hbm.xml file looks like this:
<class name="LinkAetHost" table="LINK_TABLE1_TABLE2">
<composite-id name="id" class="xx.LinkTable1Table2Id">
<key-property name="table1Fk" type="big_decimal">
<column name="TABLE1_FK" precision="22" scale="0" />
</key-property>
<key-property name="table2Fk" type="big_decimal">
<column name="TABLE2_FK" precision="22" scale="0" />
</key-property>
</composite-id>
<property name="port" type="big_decimal">
<column name="PORT" precision="22" scale="0" />
</property>
<many-to-one name="table1" class="xx.Table1" update="false" insert="false" fetch="select">
<column name="TABLE1_FK" precision="22" scale="0" />
</many-to-one>
<many-to-one name="table2" class="xx.Table2" update="false" insert="false" fetch="select">
<column name="TABLE2_FK" precision="22" scale="0" />
</many-to-one>
</class>
Code between sessionFactory.openSession() and session.close():
Table1 table1 = new Table1();
table1.setName("test");
Session session = DAO.getCurrentSession();
session.beginTransaction();
Example ex = Example.create(table1).ignoreCase().enableLike(MatchMode.START).excludeZeroes();
List<Test1> result;
Criteria criteria = session.createCriteria(Test1.class);
criteria = criteria.addOrder(Order.asc("name"));
criteria = criteria.add(ex);
result = criteria.list();
for (Test1 t : result) { // initialize all Permissions
Set<LinkTest1Test2> test2 = a.getLinkTest1Test2();
for(LinkTest1Test2 t2 : test2){
Hibernate.initialize(t2.getTest2());
}
}
session.getTransaction().commit();
Full stack trace of any exception that occurs:
Name and version of the database you are using:
Oraclle 9i
The generated SQL (show_sql=true):
Hibernate: select this_.ID as ID12_0_, this_.NAME as NAME12_0_, from TEST_DB.TABLE1 this_ where (lower(this_.NAME) like ?) order by this_.NAME asc
Hibernate: select linktable1table20_.TABLE1_FK as TABLE11_0_, linktable1table20_.TABLE2_FK as TABLE22_0_, linktable1table20_.PORT as PORT0_ from LINK_TABLE1_TABLE2 linktable1table20_ where linktable1table20_.TABLE1_FK=?
Hibernate: delete from LINK_TABLE1_TABLE2 where TABLE1_FK=? and TABLE2_FK=? and PORT=?
Hibernate: insert into LINK_TABLE1_TABLE2 (TABLE1_FK, TABLE2_FK, PORT) values (?, ?, ?)
Hibernate: insert into LINK_TABLE1_TABLE2 (TABLE1_FK, TABLE2_FK, PORT) values (?, ?, ?)
|