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.xmlCode:
<?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.xmlCode:
<?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 !