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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:28:01+00:00 2026-06-09T04:28:01+00:00

I have 2 classes, a Room class and a Student class. A Room can

  • 0

I have 2 classes, a Room class and a Student class. A Room can have many Students, while a Student also can have many Rooms. Therefore i used @ManyToMany relationship

public class Room {
  @ManyToMany
  private Collection<Student> studentList = new ArrayList<Student>();
}

public class Student {
  @ManyToMany(mappedBy="studentList")
  private Collection<Room> roomList = new ArrayList<Room>();
}

Since i want to use 1 mapping table that is Room_Student, i was able to add a collection of Students to a Room. When I tried to add collection to a Student, hibernate did not save it.
Here it is

Collection<Student> collectionOfStudents=new ArrayList<Student>();
Room room1=(Room) session.get(Room.class, 1);
Student student1=(Student) session.get(Student.class, 1);
Student student2=(Student) session.get(Student.class, 2);
collectionOfStudents.add(student1);
collectionOfStudents.add(student2);
room1.getStudentList().addAll(collectionOfStudents)
session.update(room1);

This worked and inserted to table Room_Student

When i did

Collection<Room> collectionOfRooms=new ArrayList<Room>();
Student student1=(Student) session.get(Student.class, 1);
Room room2=(Room) session.get(Room.class, 2);
Room room3=(Room) session.get(Room.class, 3);
collectionOfRooms.add(room2);
collectionOfRooms.add(room3);
student1.getRoomList().addAll(collectionOfRooms);
session.update(student1);

it did not insert into table Room_Student for room2 and room3.
Thanks for all the replies

edited 1:
i added

public class Student {
  @ManyToMany(mappedBy="studentList",cascade={CascadeType.ALL})
  private Collection<Room> roomList = new ArrayList<Room>();
}

This

student1.getRoomList().addAll(collectionOfRooms);
session.update(student1);

did not update/insert the rooms into table

  • 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-09T04:28:03+00:00Added an answer on June 9, 2026 at 4:28 am

    In your mappings, the Room is the owning side and Student is the owned side. So you will always have to add students to a room in order to create relationships between rooms and students. Doing it the opposite way will not work, even if you use Cascading. You will always have to define a owning side in the relationship in ORM and always use that side itself to create the relationships. This link shed further light on the topic. Also, you should go through the JPA/hibernate documentations if you need to dive deeper.

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

Sidebar

Related Questions

i have created two classes Standard and Family which extend abstract class Room ,
I have the next two domain classes in a grails application: class Room {
I have 4 main classes: House, Floor, Room, Asset. An asset is in a
I have classes structured like this: Public MustInherit Class A ' several properties End
Say I have classes class A{ //code for class A } class B{ //code
If I have classes of Type A and B: public class A { public
I have three classes (House, Room, Address) which are persistent (Mapped with JPA/Hibernate) @Entity
i have three classes. 1.Class:` public class Handler { private String name; private String
I have three classes, one class loads in a configuration file into memory for
I have classes like this public class someList { public string strOne {get; set;}

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.