-->
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.  [ 1 post ] 
Author Message
 Post subject: Updating & deleting collection's item with composite id
PostPosted: Wed Jun 24, 2009 4:25 am 
Newbie

Joined: Thu Jun 12, 2008 10:52 am
Posts: 16
Hello everyone,
I'm trying to manipulate a Set which is composed of entity with composite-id. This is not the easiest thing i must say.
I have generated almost everything with the Tools.
Here is what I have : Products which contains a Set of PriceSupermarket. A PriceSupermarket contains a price, a Product, a Supermarket and a PriceSupermarketId. PriceSupermarketId has a idProduct (int) and a idSupermarket (int).

Product.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 11 f?vr. 2009 16:50:45 by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="db.Product" table="product" catalog="myshop">
        <id name="idProduct" type="java.lang.Integer">
            <column name="id_product" />
            <generator class="identity" />
        </id>
        <!-- other properties ...-->
        <set name="priceSupermarkets" inverse="true" cascade="all,delete-orphan">
            <key>
                <column name="id_product" not-null="true" />
            </key>
            <one-to-many class="db.PriceSupermarket" />
        </set>
    </class>
</hibernate-mapping>

PriceSupermarket.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 11 f?vr. 2009 16:50:45 by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="db.PriceSupermarket" table="product_supermarket" catalog="myshop">
        <composite-id name="id" class="db.PriceSupermarketId">
            <key-property name="idProduct" type="int">
                <column name="id_product" />
            </key-property>
            <key-property name="idSupermarket" type="int">
                <column name="id_supermarket" />
            </key-property>
        </composite-id>
        <many-to-one name="product" class="db.Product" update="false" insert="false" fetch="select">
            <column name="id_product" not-null="true" />
        </many-to-one>
        <many-to-one name="supermarket" class="db.Supermarket" update="false" insert="false" fetch="select">
            <column name="id_supermarket" not-null="true" />
        </many-to-one>
        <property name="price" type="big_decimal">
            <column name="price" precision="6" />
        </property>
    </class>
</hibernate-mapping>

So far, what i am doing is this :
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Product p = (Product) session.get(Product.class, 1);

Iterator<PriceSupermarket> it = p.getPriceSupermarkets().iterator();
if (it.hasNext())
{
   PriceSupermarket ps = it.next();
   Supermarket s = (Supermarket) session.get(Supermarket.class, 3);
   ps.setPrice(BigDecimal.valueOf(8888.22));
   ps.getId().setIdSupermarket(s.getIdSupermarket());
   ps.setSupermarket(s);
}
if (it.hasNext())
{
   it.next();
   it.remove();
}

session.save(p);
tx.commit();

part of SQL log wrote:
...
Hibernate: select supermarke0_.id_supermarket as id1_9_0_, supermarke0_.name as name9_0_ from myshop.supermarket supermarke0_ where supermarke0_.id_supermarket=?
Hibernate: update myshop.product_supermarket set price=? where id_product=? and id_supermarket=?
Hibernate: delete from myshop.product_supermarket where id_product=? and id_supermarket=?

The deletion seems ok thanks to cascade delete-orphan and the price is updated when it changed. But the supermarket never get updated and worst, somtimes i'm getting this error :
Quote:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Big question is : how a Set like that should be handled ? Is delete-orphan a good thing ? Does the composite-id a good thing to use ? (i'm using MySQL 5)
Thank you !


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

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.