Reputation: 493
How can I add a Object to a relation and persist it ?
if i do something like that
registrationInstance.addToActionType(id:id)
I get an exception
Unique index or primary key violation: "PRIMARY_KEY_E7 ON PUBLIC.ACTION_TYPE_REGISTRATIONS(ACTION_TYPE_ID, REGISTRATION_ID)"; SQL statement: insert into action_type_registrations (action_type_id, registration_id) values (?, ?) [23001-147]
EDIT:
If I write
registrationInstance.addToActionType(ActionType.get(id))
I get the same Error, but it persists - so how can I get rid of that error?
EDIT2:
If I try to remove an object:
registrationInstance.removeFromActionType(ActionType.get(id))
I receve following error (sometimes, not allways)
Eindeutiger Index oder Primarschlüssel verletzt: "PRIMARY_KEY_E7 ON PUBLIC.ACTION_TYPE_REGISTRATIONS(ACTION_TYPE_ID, REGISTRATION_ID)" Unique index or primary key violation: "PRIMARY_KEY_E7 ON PUBLIC.ACTION_TYPE_REGISTRATIONS(ACTION_TYPE_ID, REGISTRATION_ID)"; SQL statement: update action_type_registrations set action_type_id=? where registration_id=? and action_type_idx=? [23001-147]
Upvotes: 0
Views: 239
Reputation: 5198
You must add the object itself to the relationship:
registrationInstance.addToActionType(ActionType.load(id))
registrationInstance.save()
Upvotes: 1
Reputation: 75681
It looks like it's not detecting that the instance is already in the collection. Implement sensible hashCode
and equals
methods for ActionType
so Hibernate can detect that it's already there and not try to create a duplicate record in the ACTION_TYPE_REGISTRATIONS
join table.
Upvotes: 1