Hi, all,
Is it possible to map a bidirectional relationship in a collection where the element's pk contains the parent's pk, and using cascade="all"? For the life of me, I can't make this work:
I have 2 classes
OrderInfo
serviceId (pk)
sequence (pk)
OrderInfoState
serviceId (pk)
orderInfoSequence(pk)
sequence(pk)
OrderInfo has a collection of OrderInfoStates mapped like this:
<composite-id name="id" class="pt.sgctelecom.crm.servId.ordem.SICompositeId">
_<key-property name="sequence" type="java.lang.Integer" column="sequence"/>
_<key-property name="serviceId" type="java.lang.Long" column="service_id"/>
</composite-id>
<set name="states" lazy="true" inverse="true" cascade="all" sort="unsorted" order-by="entryDate asc">
_<key>
__<column name="service_id"/>
__<column name="serviceOrder_sequence"/>
_</key>
_<one-to-many class="blah.blah2.blah3.OrderInfoState"/>
</set>
and the relationship is mapped like this on the OrderInfoState side:
<composite-id name="id" class="blah.blah2.blah3.OrderInfoStateId">
_<key-property name="orderInfoSequence" type="java.lang.Integer" column="serviceOrder_sequence"/>
_<key-property name="sequence" type="java.lang.Integer" column="sequence"/>
_<key-property name="serviceId" type="java.lang.Long" column="service_id"/>
</composite-id>
<many-to-one name="orderInfo" class="blah.blah2.blah3.OrderInfo" cascade="none" outer-join="auto" update="false" insert="false" access="property">
_<column name="service_id" not-null="true"/>
_<column name="serviceOrder_sequencia" not-null="true"/>
</many-to-one>
I'm using insert="false" update="false" because other way, hibernate will complain there are repeated columns (the "service_id" and "serviceOrder_sequence" columns, which are parte of the composite-id, and the many-to-one mapping).
I insert them as follows.
// need to get orderInfo key first, so insert it first
session.save(orderInfo);
orderState.setOrderInfo(orderInfo);
orderInfo.getStates().add(orderState);
session.flush();
Hibernate always tries to update the OrderInfoState table, instead of inserting. I get
2004-06-16 17:47:03,397 ERROR [net.sf.hibernate.impl.SessionImpl] Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
Will anyone help me with this? Am I doing something wrong?
Thank you.
|