Many to many hibernate mapping with extra columns?

10,874

solution with Annotation Case can be found here

How Do I Create Many to Many Hibernate Mapping for Additional Property from the Join Table?

Solution using xml file can be found here: Mapping same class relation

and here:

Mapping same class relation - continuation

Share:
10,874
Ramesh Kotha
Author by

Ramesh Kotha

● 10+ years of experience in object-oriented design, development, deployment and maintenance of Web and JEE applications using process methodologies ● Expert in development of applications using JEE technologies like Java, JSP, Servlets, JDBC, JNDI, and JavaMail ● Expert in developing Angular2 single page applications. ● Expert in developing Google Maps V3 / OpenLayers map based applications. ● Experience in developing and deploying applications using Tomcat, Web Logic. ● Proficiency in Oracle and MySQL ● Expertise in Various IDE’s likes Eclipse and MyEclipse. ● Experience in developing applications using three tier architectural frameworks such as MVC (Model View Controller) and SPRING framework and Hibernate. ● Experience in JQuery, Javascript, DWR, AJAX and XML. ● Experience working extensively on Windows environments ● Worked on all phases of Systems Development life cycle (SDLC) ● Prepared test case scenarios and internal documentation for validation and reporting ● Experienced in User Support and training end users for efficient use of developed applications. ● Strong knowledge of Gang of Four design Patterns like Façade, Singleton, DAO ● Versed with development methodologies namely SDLC

Updated on June 04, 2022

Comments

  • Ramesh Kotha
    Ramesh Kotha almost 2 years

    Hi i have many to many mapping with extra columns in the join table. table structure look like this.

    table vendor{vendor_id, vendor_name, vendor_password, etc...}
    table student{student_id, student_name, student_password, etc..}
    table test{test_id, test_subject, test_price,test_level, etc..}
    

    Relations as follows

    vendor to test --> many-to-many
    student to test --> many-to-many
    

    link

    table vendor_student_test{vendor_id, student_id, test_id, purchasedDate, assignedDate, writtenDate, result}
    

    i created POJO classes as follows

    1. Vendor.java
    public class Vendor {
    Long vendorId;
    Set<VendorStudentTest> tests;
    //set and get
    }
    
    1. Student.java
    public class Student{
    Long studentId;
    Set<VendorStudentTest> tests;
    //set and get
    }
    
    1. Test.java
    public class Test{
    Long test_id;
    double test_price;
    //set and get and remaining fields
    }
    
    1. VendorStudentTest.java
    public class VendorStudentTest{
    public VendorStudentTestPK vendorStudentTestPK;
    Date purchasedDate;
    Date assignDate;
    Date writtenDate;
    double result;
    //set and get accordingly
    }
    
    1. VendorStudentTestPK.java
    public class VendorStudentTestPK{
    Long vendor_id;
    Long student_id;
    Long test_id;
    }
    

    Hibernate mapping files as follows

    vendor.hbm.xml

    <set name="tests" table="vendor_student_test"
                    inverse="true" lazy="true" fetch="select">
                <key>
                    <column name="vendor_id" not-null="true" />
                </key>
                <one-to-many class="VendorStudentTest" />
            </set>
    

    vendor_student_test.hbm.xml

        <composite-id name="vendorStudentTestPK"
            class="com.onlineexam.model.VendorStudentTestPK">
    
            <key-property name="vendor" column="vendor_id"
                type="java.lang.Long" />
            <key-property name="student" column="student_id"
                type="java.lang.Long" />
            <key-property name="test" column="test_id" type="java.lang.Long" />
    
        </composite-id>
    

    student.hbm.xml

    <set name="tests" table="vendor_student_test"
                    inverse="true" lazy="true" fetch="select">
                <key>
                    <column name="student_id" not-null="true" />
                </key>
                <one-to-many class="VendorStudentTest" />
            </set>
    

    test.hbm.xml

    //this is mapped to other table 
    

    I am new to hibernate, Is this correct mapping?