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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:43:54+00:00 2026-05-12T05:43:54+00:00

I have these two class(table) @Entity @Table(name = course) public class Course { @Id

  • 0

I have these two class(table)

@Entity
@Table(name = "course")
public class Course {

    @Id
    @Column(name = "courseid")
    private String courseId;
    @Column(name = "coursename")
    private String courseName;
    @Column(name = "vahed")
    private int vahed;
    @Column(name = "coursedep")
    private int dep;
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "course_id"),  inverseJoinColumns = @JoinColumn(name = "student_id"))
    private Set<Student> student = new HashSet<Student>();
//Some setter and getter

and this one:

    @Entity
    @Table(name = "student")
    public class Student {

        @Id
        @Column(name="studid")
        private String stId;
        @Column(nullable = false, name="studname")
        private String studName;
        @Column(name="stmajor")
        private String stMajor;
        @Column(name="stlevel", length=3)
        private String stLevel;
        @Column(name="stdep")
        private int stdep;

        @ManyToMany(fetch = FetchType.LAZY)
        @JoinTable(name = "student_course"
                ,joinColumns = @JoinColumn(name = "student_id")
                ,inverseJoinColumns = @JoinColumn(name = "course_id")
        )
        private Set<Course> course = new HashSet<Course>();
//Some setter and getter

After running this code an extra table was created in database(student_course), now I wanna know how can I add extra field in this table like (Grade, Date , and … (I mean student_course table))
I see some solution but I don’t like them, Also I have some problem with them:

First Sample

  • 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-12T05:43:54+00:00Added an answer on May 12, 2026 at 5:43 am

    If you add extra fields on a linked table (STUDENT_COURSE), you have to choose an approach according to skaffman’s answer or another as shown bellow.

    There is an approach where the linked table (STUDENT_COURSE) behaves like a @Embeddable according to:

    @Embeddable
    public class JoinedStudentCourse {
    
        // Lets suppose you have added this field
        @Column(updatable=false)
        private Date joinedDate;
    
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
        private Student student;
    
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="COURSE_ID", insertable=false, updatable=false)
        private Course course;
    
        // getter's and setter's 
    
        public boolean equals(Object instance) {
            if(instance == null)
                return false;
    
            if(!(instance instanceof JoinedStudentCourse))
                return false;
    
            JoinedStudentCourse other = (JoinedStudentCourse) instance;
            if(!(student.getId().equals(other.getStudent().getId()))
                return false;
    
            if(!(course.getId().equals(other.getCourse().getId()))
                return false;
    
            // ATT: use immutable fields like joinedDate in equals() implementation
            if(!(joinedDate.equals(other.getJoinedDate()))
                return false;
    
            return true;
        }
    
        public int hashcode() {
            // hashcode implementation
        }
    
    }
    

    So you will have in both Student and Course classes

    public class Student {
    
        @CollectionOfElements
        @JoinTable(
            table=@Table(name="STUDENT_COURSE"),
            joinColumns=@JoinColumn(name="STUDENT_ID")
        )
        private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>();
    
    }
    
    public class Course {
    
        @CollectionOfElements
        @JoinTable(
            table=@Table(name="STUDENT_COURSE"),
            joinColumns=@JoinColumn(name="COURSE_ID")
        )
        private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>();
    
    }
    

    remember: @Embeddable class has its lifecycle bound to the owning entity class (Both Student and Course), so take care of it.

    advice: Hibernate team suppports these two approachs (@OneToMany (skaffman’s answer) or @CollectionsOfElements) due some limitations in @ManyToMany mapping – cascade operation.

    regards,

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

Sidebar

Related Questions

I have these two entities. One for Employees: [Table(Name = Employees)] public class Employee
I have a class that contains two methods like these: public String getFoo(Int32 a)
I have an entity class: Cabbage.java package womble; public class Cabbage { private int
I have a class split across two files. One of these is generated, the
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity
I have two entites, Person and Teacher , with a OneToMany relationship: public class
I have these two pieces of code, wich one is more readable? foreach decimal
Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate
I have seen these two approaches for constant declaration which are to be used
Do these two keywords have exactly the same effect, or is there something I

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.