【问题标题】:Many to many hibernate mapping with extra columns?带有额外列的多对多休眠映射?
【发布时间】:2012-05-12 01:49:09
【问题描述】:

您好,我在连接表中有多对多的映射和额外的列。 表结构如下所示。

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..}

关系如下

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

链接

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

我按如下方式创建了 POJO 类

  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 映射文件如下

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>

学生.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 

我是 hibernate 新手,这是正确的映射吗?

【问题讨论】:

    标签: hibernate hibernate-mapping


    【解决方案1】:

    可以在这里找到带有注释案例的解决方案

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

    使用 xml 文件的解决方案可以在这里找到: Mapping same class relation

    这里:

    Mapping same class relation - continuation

    【讨论】:

    • 所以,请参考最后两个链接。它解释了您需要知道的一切。
    • 嗨,我已经更新了映射,你能告诉我是不是正确的。
    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多