When trying to reorder a List, I have the following error :
org.hibernate.exception.ConstraintViolationException: could not update collection rows: [com.netappsid.erp.server.bo.base.BaseDiscountGroup.details#3]
Here is a simple mapping of what I got :
@IndexColumn(name="sequence")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
public List<DiscountDetail> getDetails()
{
return details;
}
Hibernate manage this relation automatically by populating the DiscountGroup_DiscountDetail table.
If I add a new item to the collection the update is done properly. In fact it is an insert, that is :
insert into DiscountGroup_DiscountDetail (DiscountGroup_id, sequence, details_id) values (?, ?, ?)
But if I modify the order, I end up with some update :
update DiscountGroup_DiscountDetail set details_id=? where DiscountGroup_id=? and sequence=?
Let's say I have the following data :
DiscountGroup_id, sequence, details_id
1, 0, , 1
1, 1, , 2
1, 2, , 0
If I try to change the order to that one (switching details_id 0 and 2) :
DiscountGroup_id, sequence, details_id
1, 0, , 1
1, 1, , 0
1, 2, , 2
Such change when persisting, will end up with these updates :
update DiscountGroup_DiscountDetail set details_id=0 where DiscountGroup_id=1 and sequence=1
update DiscountGroup_DiscountDetail set details_id=2 where DiscountGroup_id=1 and sequence=2
Since I have a unique constraint on the details_id, when updating doing the first update (details_id = 0), I end up with the following exception :
org.hibernate.exception.ConstraintViolationException: could not update collection rows: [com.netappsid.erp.server.bo.base.BaseDiscountGroup.details#3]
This is normal since updating the 2nd line details_id to 0 will conflict with currently existing 3rd row that also has 0 for the details_id value.
The solution would be to delete the entire table and refill it again. Maybe there is another way, but I can't find any other way to solve it.
Here is the complete stack trace if it can helps :
org.hibernate.exception.ConstraintViolationException: could not update collection rows: [com.netappsid.erp.server.bo.base.BaseDiscountGroup.details#3]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.BasicCollectionPersister.doUpdateRows(BasicCollectionPersister.java:222)
at org.hibernate.persister.collection.AbstractCollectionPersister.updateRows(AbstractCollectionPersister.java:1337)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:55)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:38)
Thanks in advance to help me solve this problem.
Sonny
|