-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Error when moving elements in a list (@OrderColumn)
PostPosted: Tue Sep 13, 2011 9:50 am 
Beginner
Beginner

Joined: Thu Jan 06, 2011 6:19 am
Posts: 25
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?


Top
 Profile  
 
 Post subject: Re: Error when moving elements in a list (@OrderColumn)
PostPosted: Wed Sep 14, 2011 2:48 am 
Beginner
Beginner

Joined: Thu Jan 06, 2011 6:19 am
Posts: 25
Hm, when I analyze the statement

Code:
update MyElement_MyElement set children_ID=? where MyElement_ID=? and position=?


you can see that hibernate is trying to set the children_ID to the new position instead of setting the new position to the existing children_ID which leads to the exception because there is a unique key on the children_ID column.

Example:
Code:
MyElement_ID    children_ID    position
"MyParent"         B                   0
"MyParent"         D                   1
"MyParent"         G                   2


Moving D to position 0 leads to:
Code:
update MyElement_MyElement set children_ID=D where MyElement_ID=MyParent and position=0
MyElement_ID    children_ID    position
"MyParent"         D                   0
"MyParent"         D   (!!!)           1
"MyParent"         G                   2


What can I do to avoid this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.