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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:31:06+00:00 2026-05-22T12:31:06+00:00

Hi I have a hibernate entity which has a set of another entity as

  • 0

Hi

I have a hibernate entity which has a set of another entity as its field. Something like this:

public class UserEntity implements Srializable {
    private Set<Role> roles;
}

I should keep tables in a way that at least one ADMIN user always be exist in the system. This may be done in a simple way and can be like below:

public void updateUser{
     UserEntity ue = getUser();
     if (userIsNotTheLastAdmin(ue)) {
     /** Here is a race condition **/
         roles.remove(Role.ADMIN);
         getSession().saveOrUpdate(ue);
     }              
}

But the real problem happens when we have concurrent operations. How can I perform all of the operation in an atomic way?

Thanks,
HM

  • 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-22T12:31:07+00:00Added an answer on May 22, 2026 at 12:31 pm

    since you probably dont want to lock a whole db table, which is quite an evil thing to do, you could have a field in your group table with a usercount value, then you can span your transactions over user table manipulations and the update of the corresponding field value in the group table and make sure that the usercount for specific groups does not drop below 1. since hibernate acquires write locks for updates automatically you wont have to think about manual locking strategies as described here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/transactions.html#transactions-locking

    hope that helped..

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

Sidebar

Related Questions

Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
I have a problem with a query using hibernate. The entity class EntityClass has
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have a book entity which has a OneToMany relationship set up to Document
I have an entity 'Parent' which has a Set of enums private Set<MyEnum> myEnums
I have a Hibernate entity which has one or more <many-to-one mappings, e.g. <hibernate-mapping>
I have an entity loaded by Hibernate (via EntityManager ): User u = em.load(User.class,
I have a class called SynonymMapping which has a collection of values mapped as
I have a entity which has a list of Child1 objects. The mappings seem
I have a standard Domain Layer entity: public class Product { public int Id

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.