I am using Hibernate's implementation of JPA. I have a user table with different types of users(Private | Public etc..), the user_type column specifies the types of the user.
I have a User class which is an entity that represents user table. I added
@DiscriminatorColumn(name="user_type", discriminatorType=DiscriminatorType.STRING)
on my User class and created 2 classes, PrivateUser and PublicUser each of them extending User Class with corresponding @DiscriminatorValue.
I also have PrivateCompany and PublicCompany tables which has one to many relationship PrivateUser entity and PublicUser entity respectively using a column called company_id in user table. I also have a Cascade delete on both OneToMany Relationships.
Now if I have a PrivateCompany with id 10 and a PublicCompany with ID 10 and user entries in user table for both PrivateCompany and PublicCompany like the following.
user_id | company_id | user_type
100 10 private
101 10 public
If I delete the PrivateCompany, I am ending up deleting user 101 along with 100 because the OneToMany relationship in on company_id and it does not consider the user_type column despite its mention as DiscriminatorColumn. I am looking for a way to give cascade delete functionality which accounts DiscriminatorColumn while deleting the children. I tried to create JoinColumns with multiple columns, but I can create the relationship with column name but not column value(which is my case).
Please let me know if my explanation is not clear.
Thanks in advance
I figured it out, on PrivateUser class add a annotation @SQLDelete like the following
This way I can add cascade delete on OneToMany Relationship of PrivateCompany and PrivateUser, but when cascade kicks in, it executes the query mentioned in SQLDelete annotation giving you control. This approach can also be used if you want to mark a record as inactive instead of deleting it.
Reference: Override default remove()/DELETE in JPA/Hibernate