Hi,
i have a problem when reordering elements in a list with child entities on a parent entity. Getting the children ordered works fine (I added the @OrderColumn annotation) but when moving one element in this list I always get an ConstraintViolationException.
Example:
My parent is "MyParent" which has children.
The list contains the elements: B, D, E, G, H, J
I want to move E to position 4 which results in: B, D, G, H, E, J
I logged the sql statements and saw that there are three update statements until the exception occurs, namely updating G to position 2, H to position 3 and E to position 4.
What could be wrong?
Here my code.
Entity:
Code:
@Entity
public class MyEntityImpl implements MyElement, Serializable {
@Id
protected String ID;
@Version
protected long version;
@ManyToOne(targetEntity = MyEntityImpl.class, fetch = FetchType.LAZY)
protected MyElement parent;
@OneToMany(targetEntity = MyEntityImpl.class, fetch = FetchType.LAZY)
@Cascade( {org.hibernate.annotations.CascadeType.ALL})
@Fetch(FetchMode.SELECT)
@LazyCollection(LazyCollectionOption.EXTRA)
@OrderColumn(name="position")
private List<MyElement> children;
public void insertChild(MyElement child, int position) {
((MyElementEntityImpl)child).setParent(this);
if (children.contains(child)) {
children.remove(child);
}
children.add(position, child);
}
...
I have a join table "MyElement_MyElement" which contains the MyElement_ID, a children_ID and a position.
My test:
Code:
MyElement parent = ....;
MyElement element_E = ...;
parent.insertChild(element_E, 4);
// save parent...
Exception:
Code:
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [update MyElement_MyElement set children_ID=? where MyElement_ID=? and position=?]; constraint [null];
...
Violation of UNIQUE KEY constraint 'UQ__FAV_Stor__1DD3BB3106EAD04D'. Cannot insert duplicate key in object 'dbo.MyElement_MyElement.
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:1132)
Statements:
Code:
2011-09-13 15:28:21,897 DEBUG [main] org.hibernate.SQL: update MyElement_MyElement set children_ID=? where MyElement_ID=? and position=?
2011-09-13 15:28:21,897 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [1] as [VARCHAR] - 84f87b52-5771-415e-bc8c-3cb2b90c1356 <-- (ID of G)
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [2] as [VARCHAR] - 4d98561e-d781-46e5-a258-17033492e500 <-- (ID of MyParent)
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [3] as [INTEGER] - 2
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [1] as [VARCHAR] - 93d25b97-7ad2-406a-933c-52af6ad8b08b <-- (ID of H)
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [2] as [VARCHAR] - 4d98561e-d781-46e5-a258-17033492e500 <-- (ID of MyParent)
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [3] as [INTEGER] - 3
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [1] as [VARCHAR] - 9048a2bb-8ece-44cf-a407-947e24256ad7 <-- (ID of E)
2011-09-13 15:28:21,898 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [2] as [VARCHAR] - 4d98561e-d781-46e5-a258-17033492e500 <-- (ID of MyParent)
2011-09-13 15:28:21,899 TRACE [main] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [3] as [INTEGER] - 4
--- Exception ---
Can you help me?