-->
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 can't I cascade a composite-id Object!?
PostPosted: Wed Dec 31, 2003 10:21 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
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)
------------------------------------------
MpRequisicaoSq044
Code:
<?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>


MpReqItem
Code:
<?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 1
Code:
( ... )
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();
( ... )


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 11:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is related to that
http://www.hibernate.org/116.html#A11

Take time to read the doc, this is a tricky but very useful stuff to understand when using non surrogate keys

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 11:34 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
epbernard wrote:
This is related to that
http://www.hibernate.org/116.html#A11

Take time to read the doc, this is a tricky but very useful stuff to understand when using non surrogate keys


Its tricky, in fact. :-)
I'm not using <version> or <timestamp> elements. Didn't need this king of functionality yet, so don't know what are they for.
I'm going to use the other way I presented in the forum. It's working great at the moment.


Thanks very much for the reply.


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.