As suggested by Ananasi:
Code:
Collections.swap(list, newIndex, oldIndex);
is the simplfication of what I'm trying to do.
It still throws the same unique constraint violation error.
I've done some more research on this one.
Here is what I found:
1)
When moveUp() method invokes this:
Code:
actions.remove( action );
hibernate does:
Code:
delete from zone_action where zone_id=? and idx=?
2)
When moveUp() method invokes that:
Code:
actions.add( newIndex, action );
hibernate does:
Code:
update zone_action set action_id=? where zone_id=? and idx=?
3)
When moveUp() method invokes the combination of them both (as suggested by tenwit):
Code:
actions.remove( action );
actions.add( newIndex, action );
hibernate surprisingly does:
Code:
Hibernate: update zone_action set action_id=? where zone_id=? and idx=?
What it means, Hibernate is first of all updating the newly inserted object,
and perhaps then, it'll remove the old one.
Unfortunately, there will not be any 'delete from...', because first 'update...' will violate the unique constraint.
Any suggestions how to force Hibernate to first delete old object and then insert new one?
============
To tenwit:
returning null in the method is somehow redundant.
Yes, you are right, it should be a void the method is returning, just to control the flow of the method.