I´ve got a question similar to this one:
I´d like to make Hibernate tell me whether an entity is referenced somewhere in the database by foreign keys. The background is: I want to implement a JIRA-like behavior in my application: delete a user only, if there are no references to it and else refuse the delete operation.
I´m aware that there may be performance issues, but first of all, I´d only like to know whether this is doable or if you can think of a better way to achieve this.
One way to do this is to call delete and catch following exceptions, but I believe that there must be a better or more elegant way to do this?
Greetings,
Chris
Hibernate has no concept or awareness of MySQL’s foreign keys. Some options I can think of are:
inverse=trueso that Hibernate doesn’t try and use that side for updating the database). Then when you want to delete a user just make sure the.size()property of the various relationships is 0. I believe this would be the ‘typical’ Hibernate solution.