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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:41:52+00:00 2026-05-27T23:41:52+00:00

I have this 2 classes: @Entity public class Student extends User { … @ManyToMany(mappedBy

  • 0

I have this 2 classes:

@Entity
public class Student extends User {
    ...
    @ManyToMany(mappedBy = "members", fetch = FetchType.EAGER)
    private Set<Group> groups;
    ...
    public void addGroup(Group group) {
        groups.add(group);
    }
}


@Entity
@Table(name = "Group")
public class Group implements Serializable {
    ...    
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "member_of")
    private Set<Student> members;
    ...    
    public void addMember(Student student) {
        members.add(student);
    }
}

This is the session bean which manages their relationships:

@Stateless
public class StudentManager extends AcademicManager implements StudentManagerRemote {
    ...
    @Override
    public void requestAnswer(long requestID, RequestAnswer answer) {
        Request request = entityManager.find(Request.class, requestID);
        if(answer == RequestAnswer.YES) {
            Student student = request.getStudent();
            Group group = request.getGroup();
            group.addMember(student);
            student.addGroup(group);
            entityManager.flush();
            entityManager.clear();
        }
    }
    ...
}

It does not update db. Why? I do the same for other relationship, but this is the only ManyToMany I have.
I also tried with a query, but did not work.

EDIT: I edited the code like Mr.J4mes suggested, but it still does not work.
Moreover: why are .flush() and .clear() not necessary?

  • 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-27T23:41:53+00:00Added an answer on May 27, 2026 at 11:41 pm

    When you’re calling group.getMembers(), you are just getting a copy of the Set that contains Student from the Group entity. Hence, when you update it, the Set inside your Group entity does not get updated. You have 2 choices:

    1. Get the Set from Group and set back after updating it:

      Set<Student> members = group.getMembers();
      members.add(student);
      group.setMembers(members);
      
    2. Add a method addStudent inside the Group entity & a method addGroup inside the Student entity to call in your requestAnswer method. It would be something like this:

      @Entity
      public class Group implements Serializable {
          private Set<Student> members;
      
          public void addMember(Student student) {
             this.members.add(student);
          }
      }
      
      @Stateless
      public class StudentManager extends AcademicManager implements StudentManagerRemote {
          @PersistenceContext
          private EntityManager em;
          ...
          @Override
          public void requestAnswer(long requestID, RequestAnswer answer) {
              Request request = entityManager.find(Request.class, requestID);
              if(answer == RequestAnswer.YES) {
                  Student student = request.getStudent();
                  Group group = request.getGroup();
                  group.addMember(student);
                  student.addGroup(group);
              }
          }
          ...
      }
      

    Besides, you don’t really need to call em.flush() and em.cancel().

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

Sidebar

Related Questions

Assume I have the following two classes: public class User : Entity { public
I have this classes who is mapped using Entity Framework Code First: public class
I have trouble when designing classes like this class C1 { public: void foo();
I have the following annotated Hibernate entity classes: @Entity public class Cat { @Column(name
I have two entity classes: public class Invoice { public int ID { get;
I have 2 Hibernate classes in a Spring-driven Application like these: @Entity public class
I have an abstract entity base class defined like this: public abstract class SessionItem
really strange situation I got here. I have 2 classes. @Entity public class CategoryData
I have classes like this public class someList { public string strOne {get; set;}
I have this class that have a function to load other classes and create

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.