I’ve got this mapping in my UserPO entity :
@ElementCollection
@JoinTable(name = "role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "department") })
@MapKeyJoinColumn(name = "mission")
private Map<MissionPO, DepartmentListPO> departmentsByMission = new HashMap<MissionPO, DepartmentListPO>();
I use a table DepartmentList and DepartmentListPO as a Map<MissionPO, List<DepartmentPO>> seems not to be an option with Hibernate (or JPA)
So far so good, a user can be given a mission restricted to a list of department.
Exemple :
Tom has mission 1 on department 10 and 11
My problem is that if I remove a mission :
departmentsByMission.remove(mission)
The role row that linked User/Mission/DepartmentList isn’t deleted.
I read a lot of threads and it seems bulk delete isn’t possible with ElementCollection.
I tried to use a constraint ON CASCADE DELETE on the role table on the DepartmentList foreign key, but this seems to be the inverse of what I want. Deleting a DepartmentList will delete the role.
So What I would like is to do something like entityManager.remove(departmentList) in the removeMissionmethod of the actor.
Unfortunately it seems that the PersistentContext can’t be put in the User entity.
So I’m a bit confused what would be the solution, knowing I would like to avoid a database trigger.
Thanks for any solution, I’m really stuck there.
I think this issue is caused by the data model rather than an issue with ORMs, hibernate or JPA. From my experiences, it seems like this is an unconventional approach.
A mission could be its own entity, which contains a department property that also exists as an entity. When creating a map of missions, it would be more useful to use its surrogate key from the database as the key for the map. I would encourage you to spend time reviewing your data model for additional entities that need to be mapped.