I have an entity like this
class Parent {
//...
List<Child> children
static mapping = {
//..
children joinTable: [name:'ParentChild', key:'ParentID', column:'ChildID'],
cascade: 'all', lazy: false, indexColumn: [name: 'OrderIndex', type: Byte]
}
}
How can I re-order and save changes to my database?
I have tried the code below:
@Transactional
Parent reOrderChildren(long parentId, CustomChildrenComparator comparator) {
Parent parent = Parent.findById(parentId)
parent.children.sort(comparator)
parent.save()
}
but the result is SqlException
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_ParentChild'. Cannot insert duplicate key in object 'ParentChild'. The duplicate key value is (15, 1761).
But database does not contain any duplicates. As far as I understand hibernate try to insert new records in ParentChild table instead of update indexColumns of existing ones