Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:25+00:00 2026-05-23T06:48:25+00:00

I´ve got a question similar to this one: How to find all foreign keys?

  • 0

I´ve got a question similar to this one:

How to find all foreign keys?

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:48:26+00:00Added an answer on May 23, 2026 at 6:48 am

    Hibernate has no concept or awareness of MySQL’s foreign keys. Some options I can think of are:

    • Map both sides of the relationship in Hibernate (make sure that to mark them with inverse=true so 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.
      • Cons – Once you have that side of the relationship in your objects it can be tempting to use it. Two-way associations can be tricky and need to be well understood.
    • Query each relationship checking to see if the user you are trying to delete is in their set. This is the least intrusive approach. You can use a count query to avoid having to send the entities into Java.
      • Cons – This is the most inefficient approach.
    • Use Native SQL If you were to try the delete and catch the exceptions you would already be relying on SQL (some databases don’t have the concept of foreign keys). You’re better off just issuing the query in native SQL at that point.
      • Cons – This adds a dependency in your program to SQL.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got a question for you all! I'm trying to make a navigation system for
I have a similar question to this one : It's very easy in py-amqp/flopsy
my question is similar to this one: OpenCV: Detect blinking lights in a video
This is a similar question to this one . I want to export a
I previously asked a similar question on this topic a while back and got
(In case this seems familiar: I asked a different question similar to this one
I've been looking around for a similar question, but I couldn't find one. I'm
My question is similar to this one: Compare rows and get percentage However, little
My question, while at first somewhat similar to this one , seems to be
My question is similar to this one but I'm having some problems with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.