2
您好,我有多對多的連接表中的額外列映射。 表結構看起來像這樣。許多到多個Hibernate映射與額外的列?
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類如下
- Vendor.java
public class Vendor { Long vendorId; Set<VendorStudentTest> tests; //set and get }
- Student.java
public class Student{ Long studentId; Set<VendorStudentTest> tests; //set and get }
- Test.java
public class Test{ Long test_id; double test_price; //set and get and remaining fields }
- VendorStudentTest.java
public class VendorStudentTest{ public VendorStudentTestPK vendorStudentTestPK; Date purchasedDate; Date assignDate; Date writtenDate; double result; //set and get accordingly }
- 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>
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
我是新來的hibernate,這是正確的映射?
您好我有使用xml映射... –
因此,請參閱後兩個lin KS。它解釋了你需要知道的一切。 –
謝謝,我做... –