0

I'm trying to do a little exercise about hibernate and relations with xml and every works fine but now I wonder how to implement extra columns on my join table but I can't find a way.

This is my code:

Grades hbm xml

<hibernate-mapping>
    <class name="Grade" table="Grades" catalog ="test">
        <id name="id">
            <column name = "id"/>
            <generator class="increment"/>
        </id>
        <property name ="name"></property>
        <property name ="code"></property>  
    </class>
</hibernate-mapping>

Students hbm xml

<hibernate-mapping>
    <class name="Student" table="Students" catalog ="test">
        <id name="id">
            <column name = "student_id"/>
            <generator class="increment"/>
        </id>
        <property name ="name"></property>
        <property name="years" type ="integer"></property>
        <set name="grade" table="student_grades" cascade="all">
            <key column="student_id" not-null="true" />
            <many-to-many column="grade_id" class="Grade"/>           
        </set>  
    </class>
</hibernate-mapping>

Grade.java

public class Grade implements Serializable{
    
    private Long id;
    private String name;
    private String code;

    public Grade () {
        
    }
    
    public Grade (String name, String code) {
        this.name= name;
        this.code= code;
    }

    getters and setters

Student.java

public class Student implements Serializable{
    
    private Long id;
    private String name;
    private int years;
    private Set<Grade> grade;
    
    public Student() {
        
    }
    
    public Student(String name, int years, Set<Grade> grade) {
        super();
        this.name= name;
        this.years= years;
        this.grade= grade;
    }

Main

public class Main {
    public static void main(String[] args) {
        Configuration cfg =new Configuration().configure();
        SessionFactory sessionFactory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
        Session session = null;
        Transaction tx = null;
        
        try {
            session = sessionFactory.openSession();
            tx = session.beginTransaction();        
            
            Grade m1 = new Grade("Test A", "01");
            session.save(m1);

            HashSet<Grade> set1 = new HashSet<Grade>();
            set1.add(m1);
            Student a = new Student("Richard", 26, set1);
            session.save(a);
 
            ...

With this I have a table named "student_grades" which has the student_id and the grade_id and in which grades are every student, but I also wanna to have on that table the grade name and student name. Is the any way?

Thank you

Weenhallo
  • 324
  • 2
  • 8

1 Answers1

0

As far as I know, additional column on join tables are not possible with many-to-many relations. I guess you need two one-to-many relations and add the student_grades manually inside your database structure.

Additionally, maybe have a look here: Mapping many-to-many association table with extra column(s)

Stibb84
  • 106
  • 5