Hibernate中many-to-many

//测试类Test:
public class Test {
	public static void main(String[] args) {
		//实例化Test1对象;
		Test1 test1=new Test1();
		test1.setT1Id(1);
		test1.setT1Name("zhao1");
        //实例化Test2对象;
		Test2 test2=new Test2();
		test2.setT2Id(1);
		test2.setT2Name("wo1");
        //实例化Test1ManyTest2Id对象;
		Test1ManyTest2Id t1_t2_id=new Test1ManyTest2Id();
		//数据库中组合组合主键;
		t1_t2_id.setTest1(test1);
		t1_t2_id.setTest2(test2);
        //实例化Test1ManyTest2对象
		Test1ManyTest2 t1_t2=new Test1ManyTest2();
		t1_t2.setId(t1_t2_id);
		//执行存储;
		Session session=HibernateSessionFactory.getSession();
		Transaction tr=session.beginTransaction();
		session.save(test1);
		session.save(test2);
		session.save(t1_t2);
		tr.commit();
		HibernateSessionFactory.closeSession();
		
		System.out.println("save over =====");
	}

}

  本测试有三张表TEST1,TEST2,TEST1_MANY_TEST2;

  Test1.hbm.xml:

<hibernate-mapping>
    <class name="hibernate.Test1" table="TEST1" schema="SCOTT">
        <id name="t1Id" type="long">
            <column name="T1_ID" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="t1Name" type="string">
            <column name="T1_NAME" length="20" not-null="true" />
        </property>

        <set name="test1ManyTest2s" table="TEST1_MANY_TEST2" cascade="save-update" inverse="true">
            <key>
                <column name="T1_ID" precision="10" scale="0" not-null="true" />
            </key>
            <many-to-many class="hibernate.Test2" column="T2_ID" />
        </set>
    </class>
</hibernate-mapping>

   Test1.hbm.xml:

<hibernate-mapping>
    <class name="hibernate.Test2" table="TEST2" schema="SCOTT">
        <id name="t2Id" type="long">
            <column name="T2_ID" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="t2Name" type="string">
            <column name="T2_NAME" length="20" not-null="true" />
        </property>
        <set name="test1ManyTest2s" table="TEST1_MANY_TEST2" cascade="save-update" inverse="true">
            <key>
                <column name="T2_ID" precision="10" scale="0" not-null="true" />
            </key>
            <many-to-many class="hibernate.Test1" column="T1_ID" />
        </set>
    </class>
</hibernate-mapping>

   Test1ManyTest2.hbm.xml:

<hibernate-mapping>
    <class name="hibernate.Test1ManyTest2" table="TEST1_MANY_TEST2" schema="SCOTT">
        <composite-id name="id" class="hibernate.Test1ManyTest2Id">
            <key-many-to-one name="test1" class="hibernate.Test1">
                <column name="T1_ID" precision="10" scale="0" />
            </key-many-to-one>
            <key-many-to-one name="test2" class="hibernate.Test2">
                <column name="T2_ID" precision="10" scale="0" />
            </key-many-to-one>
        </composite-id>
    </class>
</hibernate-mapping>

   数据库Oracle中的表:

   有三张表TEST1,TEST2,TEST1_MANY_TEST2;

   注意:1.每张表各有主键 2.其中TEST1_MANY_TEST2是组合主键;

 

你可能感兴趣的:(oracle,xml,Hibernate)