日期:2014-05-17  浏览次数:20731 次

HIbernate关系映射
Idcard.hbm.xml:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.xyy.domain.Idcard">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="foreign">
                <param name="property">person</param>
            </generator>
        </id>
        <property name="validate" type="java.util.Date">
            <column name="validate"/>
        </property>
        <!-- 配置one-to-one
        constrained:不配置,则不会自动生成外键列,配置了,则会生成外键列
         -->
        <one-to-one name="person" constrained="true"/>
    </class>
</hibernate-mapping>


Person.hbm.xml:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.xyy.domain.Person">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="assigned"/>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name"/>
        </property>
        <!-- 配置one-to-one -->
        <one-to-one name="idcard" />
    </class>
</hibernate-mapping>


TestMain.java:

            Person p1=new Person();
            p1.setId(2);
            p1.setName("scott1");
             
            Idcard idcard=new Idcard();
            idcard.setValidate(new Date());
            idcard.setPerson(p1);