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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:13:34+00:00 2026-05-28T03:13:34+00:00

while I wrote this question I figured out a solution but I’m going to

  • 0

while I wrote this question I figured out a solution but I’m going to ask anyway. Maybe there’s an easier way to do it.

I want to make an association table from two tables.
Let’s call them Student and Course. Both have an id and an unique name.
What I want is a third table (student_id to course_id) wich is automatically updated when i insert a student who’s attending courses.

It is almost the following example i found here: Hibernate Many-to-Many with Annotations but i changed the @Table annotation to
@Table(name = "STUDENT",
uniqueConstraints = {@UniqueConstraint(columnNames = {"STUDENT_NAME" })})

The same for Courses.

I insert a first student

Set<Course> courses = new HashSet<Course>();
courses.add("Math); courses.add("Science");

Student alice = new Student("alice", courses);

Here comes another Student

Student bob= new Student("bob", courses);

And it crashes becauses the courses science and math already exists in the table courses and the student_to_course table will not be updated.

What I’m doing know is to query for a course with this name in the course table. If there is one i add it to the students course otherwise add a new one.

List<Course> list = session.createCriteria(Course.class)
            .add(Restrictions.eq("courseName", "Maths")).list();
if (list.size() > 0){
   courses.add(list.get(0));
}
else {
    courses.add(new Course("Maths"));           
}
s.setCourses(courses);

So this works, but I think there must be a better way to do it.
Is there a way to tell hibernate to add the courses to the student from the courses table if there already is a course with the same name in the 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-05-28T03:13:35+00:00Added an answer on May 28, 2026 at 3:13 am

    You’re doing it correctly, and there’s no other way.

    Except this scenario is not very realistic. Most of the time, the list of available courses is known in advance, and you may only pick among the available courses when you create a student. The UI will typically propose a multi-select box where you’ll have to select courses, and you won’t refer to the chosen courses using their name, but using their ID (or using the Course entity directly).

    The method will thus look like this:

    public Student createStudent(String studentName, Set<Long> courseIds)
    

    or like this:

    public Student createStudent(String studentName, Set<Course> courses)
    

    In the first case, the easiest and fastest way to get a set of course is to loop over the course IDs and call session.load(courseId) to get a reference to the course, without even needing to hit the database.

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

Sidebar

Related Questions

There are some other variations of this question here at SO, but please read
Let's say I accidentally wrote this: do { } while (true); ...and then ran
Jeff Atwood wrote about this here , and while I understand the theoretical performance
While playing around with regexps in Scala I wrote something like this: scala> val
I wrote a while loop in a function, but don't know how to stop
A while ago I wrote an app that relied on XML coming out of
A while back I wrote a simple python program to brute-force the single solution
In this question, octy wrote : BTW, if you save on a background thread,
I hardly know how to state this question, let alone search for answers. But
(there is a follow up to this question here ) I am working on

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.