Hibernate version:
3.0.1
Mapping documents:
<class name="TRecall" table="Recall_b" optimistic-lock="version">
<id name="id" column="RecallID" >
<generator class="increment"/>
</id>
<version name="version" />
<property name="year" column="RecallYear"></property>
<set name="items" table="Recall_Article_Store_b" >
<key column="recall_id" unique="true"/>
<composite-element class="be.uchrony.recall.db.TItems">
<many-to-one name="article" column="article_id" />
<many-to-one name="store" column="store_id" />
<many-to-one name="actions" column="actions_id" />
<property name="quantity" column="quantity" />
<property name="unitCode"></property>
<property name="modificationDate" column="ModificationDate"></property>
<property name="encodingBy" column="EncodingBy"></property>
</composite-element>
</set>
</class>
Code between sessionFactory.openSession() and session.close():
Transaction tx = HibernateUtils.getSession().beginTransaction();
TRecall recall = new TRecall();
List articles = HibernateUtils.getSession()
.createQuery("FROM TArticle").list();
List stores = HibernateUtils.getSession().createQuery("FROM TStore ")
.list();
TItems item = new TItems();
item.setArticle((TArticle)articles.get(1));
item.setStore((TStore)stores.get(0));
recall.getItems().add( item );
HibernateUtils.saveOrUpdate(recall);
tx.commit();
Full stack trace of any exception that occurs:
Violation of PRIMARY KEY constraint 'PK_Recall_Article_Store_b'. Cannot insert duplicate key in object 'Recall_Article_Store_b'.
Name and version of the database you are using:
SQL Server
The generated SQL (show_sql=true):
10:42:21,015 DEBUG SQL:324 - insert into Recall_b (version, RecallYear, RecallNB, RecallVersion, CreateDate, CreateUser, Names_id, Problems_id, Origins_id, comments_id, GlobalActions_id, RecallStatus_id, RecallID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
10:42:21,031 DEBUG SQL:324 - select tarticle0_.id as id, tarticle0_.version as version3_, tarticle0_.name as name3_, tarticle0_.price as price3_ from Article tarticle0_
10:42:21,046 DEBUG SQL:324 - select tstore0_.id as id, tstore0_.version as version5_, tstore0_.name as name5_ from Store tstore0_
10:42:21,062 DEBUG SQL:324 - update Recall_b set version=?, RecallYear=?, RecallNB=?, RecallVersion=?, CreateDate=?, CreateUser=?, Names_id=?, Problems_id=?, Origins_id=?, comments_id=?, GlobalActions_id=?, RecallStatus_id=? where RecallID=? and version=?
10:42:21,093 DEBUG SQL:324 - insert into Recall_Article_Store_b (recall_id, article_id, store_id, actions_id, quantity, unitCode, ModificationDate, EncodingBy) values (?, ?, ?, ?, ?, ?, ?, ?)
10:42:21,109 DEBUG SQL:324 - update Recall_b set version=?, RecallYear=?, RecallNB=?, RecallVersion=?, CreateDate=?, CreateUser=?, Names_id=?, Problems_id=?, Origins_id=?, comments_id=?, GlobalActions_id=?, RecallStatus_id=? where RecallID=? and version=?
10:42:21,109 DEBUG SQL:324 - delete from Recall_Article_Store_b where recall_id=? and article_id=? and store_id=? and actions_id=? and quantity=? and unitCode=? and ModificationDate=? and EncodingBy=?
10:42:21,125 DEBUG SQL:324 - insert into Recall_Article_Store_b (recall_id, article_id, store_id, actions_id, quantity, unitCode, ModificationDate, EncodingBy) values (?, ?, ?, ?, ?, ?, ?, ?)
I've got a ternary association between table Recall / Store and Article (called Recall_Article_Store_b )
Anyone can help me ?
|