Using Hibernate : 3.3.3
I think hibernate can do this, I have just not been able to figure out how.
I have two Tables
Parent.java
Code:
public class Parent implements Serializable {
private Long id;
private List<Child> children;
}
Child.javaCode:
public class Child implements Serializable {
private Long id;
private Long parent_id;
private Integer sequenceNumber;
}
There is a unique constraint on (parent_id, sequenceNumber)
sequenceNumber is managed by Hibernate based on Child's position in the list located in Parent DTO.
Through the front end, user can add multiple Child DTOs to the Parent DTO as well as resequence them on the same screen.
Here goes the problem....
Parent has Child records with sequenceNumber 1 and 2. User adds a new Child record to the list at position 2. On commit the newly added Child record gets position 2 and old Child record at position 2 should now be to position 3. All this happens in the one Transaction. This causes unique constraint violation and blows up.
How to solve this without removing unique constraint as well as splitting the operations in to two different Transactions.
Thanks for your Help.