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

  • Home
  • SEARCH
  • 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 7906911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:10:57+00:00 2026-06-03T11:10:57+00:00

I have 2 Hibernate objects Group and User which are related with a ManyToMany

  • 0

I have 2 Hibernate objects Group and User which are related with a ManyToMany relationship.

I use the following mapping

Group.java

private Set<User> users = new HashSet<User>(0);

...

@ManyToMany(cascade = CascadeType.ALL)  
@JoinTable(name = "user_group",
joinColumns = { @JoinColumn(name = "GROUP_ID") },
inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
public Set<User> getUsers() {
    return this.users;
}
...

User.java

private Set<Group> groups = new HashSet<User>(0);

...


@ManyToMany(mappedBy = "users") 
public Set<Group> getGroups() {
    return this.groups;
}

...

DAO.java

public User addUserToGroup (Integer groupId , Integer  userId){
    Group group = (Group) getBeanById(groupId, Group.class); //Here I get the right group
    User user = (User) getBeanById(userId, User.class);//Here I get the right user
    group.getUsers().add(user);
    user.getGroups().add(group);
    save(group);        
    return user;
}

The save method

public EntityBase save(EntityBase transientInstance) {
    Session session = HibernateUtil.getCurrentSession();
    log.debug("persisting  instance");
    try {
        session.saveOrUpdate(transientInstance);
        log.debug("persist successful");
        return transientInstance;
    } catch (RuntimeException re) {
        log.error("save failed", re);
        throw re;
    }
}

My problem is when I add a user to a groupe, no record is added to the association table user_group.

NB : The begin and commit statements are called in a filter Servlet

  • 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-06-03T11:10:58+00:00Added an answer on June 3, 2026 at 11:10 am

    The code above does what it should do.
    My problem was at the display of the list of the users, Hibernate detect a dirty users collections and delete it. What I ended up doing is to change the ownership of the relationship to Group instead of the User, and all behaved normally.

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

Sidebar

Related Questions

I have a hibernate mapping with custom collection types in it. java: class Policy
I have an application that uses Hibernate for its domain objects. One part of
I have a hibernate object that gets detached and transferred to a thick java
We have an object, A, which contains another object, B. We have Hibernate calling
I have a spring-hibernate application which is failing to map an object properly: basically
I have inherited a bit of Java code that uses Hibernate. Some of the
I have some Hibernate entities that I want to group by paramType and display
I have couple of tables, where I cannot use hibernate mappings to define associations
I have a bunch of Hibernate mapped objects in my Spring MVC app. Default
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager

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.