I a bit confused with the folllowing situation.
When I put
cascade="save-update" for
MpReqItem in the
MpRequisicaoSq044 mapping file just like I do below, the
Example 2 code doesn't work and gives me the following error:
Code:
ERROR: Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:672)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:625)
( ... )
But if I remove the
cascade="save-update" and use the
Example 1 code, it works correctly by inserting my
MpRequisicaoSq044 and the "set" of
MpReqItem.
Why doesn't
Example 2 work with
cascade="save-update" in
MpRequisicaoSq044?
Can't I cascade objects with composite-id's!?
Please help. Don't understand the problem.
------------------------------------------
Mapping files (generated with R3)
------------------------------------------
MpRequisicaoSq044Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="vo.MpRequisicaoSq044"
table="mp_requisicao_sq044"
>
<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>
<property
name="estado"
type="java.lang.String"
column="estado"
length="1"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to MpReqItem -->
<set
name="mpReqItems"
lazy="true"
inverse="true"
cascade="save-update"
>
<key>
<column name="mp_requisicao_fk" />
</key>
<one-to-many
class="vo.MpReqItem"
/>
</set>
</class>
</hibernate-mapping>
MpReqItemCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="pt.comseal.arsol.vo.MpReqItem"
table="mp_req_item"
>
<composite-id name="comp_id" class="vo.MpReqItemPK">
<!-- bi-directional many-to-one association to MpRequisicaoSq044 -->
<key-many-to-one
name="mpRequisicaoSq044"
class="vo.MpRequisicaoSq044"
>
<column name="mp_requisicao_fk" />
</key-many-to-one>
<!-- bi-directional many-to-one association to Mp -->
<key-many-to-one
name="mp"
class="vo.Mp"
>
<column name="mp_fk" />
</key-many-to-one>
</composite-id>
<property
name="quantidade"
type="int"
column="quantidade"
length="4"
/>
<!-- associations -->
</class>
</hibernate-mapping>
-------------------
Testing code
-------------------
Example 1Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(mp_req);
Iterator it = mp_req.getMpReqItems().iterator();
while(it.hasNext()) {
MpReqItem mp_req_item = (MpReqItem)it.next();
session.save(mp_req_item);
}
transaction.commit();
( ... )
Example 2 (doesn't work with cascade="save-update")Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(mp_req);
transaction.commit();
( ... )