复合主键

<?xml

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.javaee.pojo.TUser" table="T_USER" schema="SCOTT">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="32" />
        </property>
        <set name="recodes" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="USERID" precision="22" scale="0" not-null="true"/>
            </key>
            <one-to-many class="com.javaee.pojo.Recode" />
        </set>
        <set name="goodses" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="ID" precision="22" scale="0" not-null="true" unique="true" />
            </key>
            <one-to-many class="com.javaee.pojo.Goods" />
        </set>
    </class>
</hibernate-mapping>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.javaee.pojo.Recode" table="RECODE" schema="SCOTT">
        <composite-id name="id" class="com.javaee.pojo.RecodeId">
            <key-many-to-one name="TUser" class="com.javaee.pojo.TUser">
                <column name="USERID" precision="22" scale="0" />
            </key-many-to-one>
            <key-many-to-one name="goods" class="com.javaee.pojo.Goods">
                <column name="GOODSID" precision="22" scale="0" />
            </key-many-to-one>
        </composite-id>
    </class>
</hibernate-mapping>
 

 

<hibernate-mapping>
    <class name="com.javaee.pojo.Goods" table="GOODS" schema="SCOTT">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <many-to-one name="TUser" class="com.javaee.pojo.TUser" update="false" insert="false" fetch="select">
            <column name="ID" precision="22" scale="0" not-null="true" unique="true" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="32" />
        </property>
        <set name="recodes" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="GOODSID" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.javaee.pojo.Recode" />
        </set>
    </class>
</hibernate-mapping>

 引用关系 goods-->user  goods-->recode goods-->user

外键类pKRecodeId(useid,goodsid)

描述复合键使用<composite-id>

级联保存 cascade="save-update"|"all-delete-orphan" 在one放的<set cascade="">配置

package com.javaee.pojo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Userdao udao = new Userdao();
		Goodsdao gdao = new Goodsdao();
		RecodeDao rdao = new RecodeDao();
		Recode r = new Recode();
		TUser u = new TUser();
		u.setId(new Long(1));
		u.setName("浪子");
		
		Set<Goods> goodses = new HashSet<Goods>();
		Goods g1 = new Goods();
		g1.setId(new Long(3));
		g1.setName("book");
		gdao.save(g1);
		goodses.add(g1);
		u.setGoodses(goodses);
		udao.save(u);
		RecodeId rid = new RecodeId();
		rid.setGoods(g1);
		rid.setTUser(u);
		r.setId(rid);
		rdao.save(r);

	}

}
总结:
使用复合主键的场合:在设计表的时候避免使用复合主键--->主键不应该有业务逻辑,适合旧表维护,采用一个外键类,复合键来描述旧表中的代表业务的主键

 

你可能感兴趣的:(xml,.net,Hibernate,javaee,MyEclipse)