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 6869687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:35:21+00:00 2026-05-27T03:35:21+00:00

I have java application with web interface (Spring MVC) using Hibernate. I have quite

  • 0

I have java application with web interface (Spring MVC) using Hibernate. I have quite simple DB scheme here with a Group entity which you can add/delete on one webpage. Then there is another Entity having Group as field therefor Entity has a FK to Group. Note that there might be generally many other entities referencing my Group.

If I want to delete Group object it might fail on FK constraint – the group is referenced by some other entity.

Is there a way how to perform a check wheter my Group is referenced and the delete will fail rather then performing the delete and catching the exception? What is the best practice here?

Thanks

NOTE: Just to be clear, simple select is not a solution since the Group might be referenced generaly by many other tables, not only one.

  • 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-27T03:35:21+00:00Added an answer on May 27, 2026 at 3:35 am

    Checking before deleting is less efficient, and might still lead to a constraint violation if some link is added by another transaction between the moment you check and the moment you delete.

    On the other hand, it allows displaying a more specific error message to the user (like “the group is still referenced by a FooBar”).

    Just do what you feel is best in your specific case. Note however that if some exception happens, the whole transaction should be rolled back, and the session should be closed, because such an exception leaves the session in an inconsistent state. Moreover, it’s not the delete, but the flush or the commit that will throw the exception.

    Now, if you want to check, there is no magical solution. You might have bidirectional associations, and you could thus check that group.getUsers() is emtpy (although this will fetch all the users of the group). Or you could execute dedicated queries like select count(user.id) from User user where user.group = :group to know if the group is still referenced. But you need to do one of those for every possible reference to the group.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring MVC based Web Application with Hibernate. Following is the directory
I have a java application that has Web Services published using Axis. With the
I have a Java based web-application using Java Server Faces and Facelets. I am
I have a web application developped in Java 1.5 with Spring framework. Application contains
I have existing Java application that is using Acegi for authentication/authorization. Our new web
I have a Java web application at my work and I'd like simplify how
I have a Java-JSF Web Application on GlassFish, in which I want to use
Is it possible to have a Java/J2EE web application integrate/communicate with SharePoint to load,
I have a web application (Java, Websphere, JSP) which allows co-workers to register visitors
We have a java web service application that uses log4j to do logging. An

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.