许多到许多Hibernate映射与额外的列? [英] Many to many hibernate mapping with extra columns?

查看:107
本文介绍了许多到许多Hibernate映射与额外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有多对多的连接表中的额外列映射。
表结构看起来像这样。

 表供应商{vendor_id,vendor_name,vendor_password等} 
table student {student_id,student_name,student_password等}
table test {test_id,test_subject,test_price,test_level等。}

关系如下

  vendor to test  - >多对多
学生来测试 - >多对多

link

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

我创建了POJO类如下:
$ b


  1. Vendor.java




 公共类供应商{
Long vendorId;
Set< VendorStudentTest>测试;
//设定并获得
}





  1. Student.java




 公开课学生{
Long studentId;
Set< VendorStudentTest>测试;
//设定并获得
}





  1. Test.java




  public class Test {
Long test_id;
double test_price;
//设置并获取剩余字段
}





  1. VendorStudentTest.java




  public class VendorStudentTest {
public VendorStudentTestPK vendorStudentTestPK;
日期购买日期;
日期assignDate;
日期书写日期;
双倍结果;
//设置并相应得到
}




<


  public class VendorStudentTestPK {
Long vendor_id;
Long student_id;
Long test_id;

code $ <$ $ p

Hibernate映射文件如下



vendor.hbm.xml

 < set name =tests表=vendor_student_test
inverse =truelazy =truefetch =select>
< key>
< column name =vendor_idnot-null =true/>
< / key>
<一对多等级=VendorStudentTest/>
< / set>

vendor_student_test.hbm.xml

 < composite-id name =vendorStudentTestPK
class =com.onlineexam.model.VendorStudentTestPK>

< key-property name =vendorcolumn =vendor_id
type =java.lang.Long/>
type =java.lang.Long/>
< key-property name =testcolumn =test_idtype =java.lang.Long/>

< / composite-id>

student.hbm.xml

 < set name =teststable =vendor_student_test
inverse =truelazy =truefetch =select>
< key>
< column name =student_idnot-null =true/>
< / key>
<一对多等级=VendorStudentTest/>
< / set>

test.hbm.xml

  //这是映射到其他表

,这是否正确的映射?

解决方案

解决方案与注释案例可以在这里找到



如何从连接表创建多个到多个Hibernate映射的附加属性?使用xml文件的解决方案可以在这里找到:
$ b a href =https://stackoverflow.com/questions/3813941/mapping-same-class-relation>映射相同的类关系



和这里:



映射同类关系 - 延续


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?

解决方案

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

这篇关于许多到许多Hibernate映射与额外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆