RKSinha
RKSinha

Reputation: 61

Deleting a subclassed overlapping JPA entity in a JOINED Strategy

I want to model something like the following

A super class User with JOINED inheritance strategy.
User has two subclasses HumanUser and BotUser.
HumanUser has two subclasses NonAdminUser and AdminUser
NonAdminUser has two subclasses Citizen and Operator
AdminUser has two subclasses SiteAdministrator and UserAdministrator
BotUser has one subclass System

I want to model an overlapping hierarchy where a user with a given userId can be one or more of Citizen, Operator, SiteAdministrator and UserAdministrator.

How can this be achieved in JPA?

In my application, after creating one leaf-level entity (say Citizen), when I want to create another overlapped entity, an error is thrown saying due to primary key constraint, persistence cannot be done.

Also, if overlapping is possible, how to delete just the information at the leaf-level. For e.g. only delete Citizen specific data, but let SiteAdministrator data remain.

Any thoughts?

Upvotes: 0

Views: 234

Answers (1)

kostja
kostja

Reputation: 61558

Do not put the new class on top of the existing hierarchy, and it is technically not possible for a subclass of User to be both a Citizen and a System, since Java does not support multiple inheritance.

I would implement a class like UserRoles with a ManyToMany relation to User (favouring composition over inhertance). Then you can remove a single subclass of User from the UserRoles and from the DB without unwanted side-effects.

@ManyToMany
private List<User> roles = new ArrayList<User>();

Upvotes: 1

Related Questions