关闭

87-----hibernate的几种映射关系总结

标签: java
104人阅读 评论(0) 收藏 举报

1.一对一(外键)

<class name="cn.jbit.hiberntedemo.entity.Resume1" table="RESUME1">
    <id name="resid" type="java.lang.Integer">
        <column name="RESID" precision="6" scale="0" />
        <generator class="assigned" />
    </id>
    <many-to-one name="users1"
    class="cn.jbit.hiberntedemo.entity.Users1"
    column="RESUSERID" cascade="all" unique="true"/>
    <property name="resname" type="java.lang.String">
        <column name="RESNAME" length="40"/>
    </property>
    <property name="rescardno" type="java.lang.String">
        <column name="RESCARDNO" length="40"/>
    </property>
</class>

一对一外键映射:让外键唯一

主表<one-to-one>

子表<many-to-one unique="true"/>

一对一主键映射:让子表主键使用主表中的主键

主表<one-to-one>

子表<id><generatorclass="foreign"><param name=”property”></></></>

     <one-to-one name=""constrained="true">


2.一对一:按照主键映射

<class name="cn.jbit.hiberntedemo.entity.Users2" table="USERS2">
    <id name="userid" type="java.lang.Integer" column="USERID">
        <generator class="foreign">
            <param name="property">resume2</param>
        </generator>
    </id>
    <one-to-one name="resume2"
    class="cn.jbit.hiberntedemo.entity.Resume2"
    constrained="true"/>
    <property name="username" type="java.lang.String">
        <column name="USERNAME" length="40" not-null="true" />
    </property>
    <property name="userpass" type="java.lang.String">
        <column name="USERPASS" length="40" />
    </property>
</class>


3.组件映射

<class name="cn.jbit.hiberntedemo.entity.Empinfo" table="EMPINFO">
    ......
    <component name="ehome"
        class="cn.jbit.hiberntedemo.entity.EmpHomeAddress">
        <parent name="empinfo" />
        <property name="ehomestreet" type="java.lang.String" 
                         column="EHOMESTREET" />
        <property name="ehomecity" type="java.lang.String“
                         column="EHOMECITY" />
        <property name="ehomeprovince" type="java.lang.String" 
                         column="EHOMEPROVINCE" />
        <property name="ehomezipcode" type="java.lang.String"   
                         column="EHOMEZIPCODE" />
    </component>
</class>

创建CUSTOMER表,表中有电话(TEL)、年龄(AGE)、姓氏(FIRSTNAME)、名字(LASTNAME)字段。

创建Customer类,类中有电话(tel)、年龄(age)、姓名(name)属性,其中name属性是Name类型;创建Name类,类中有姓氏(firstname)、名字(lastname)属性。

CUSTOMER表中的FIRSTNAMELASTNAME字段与Customer类中的name属性对应。

保存Customer对象,并按主键加载Customer对象,打印telagename属性。



4.多对一单向关联关系

<p><span style="color:#061324;">public class </span><span style="color:#061324;">Emp</span><span style="color:#061324;">implements </span><span style="color:#061324;">Serializable</span><span style="color:#061324;">{    </span></p><p><span style="color:#061324;">   private Integer </span><span style="color:#061324;">empNo</span><span style="color:#061324;">;</span></p><p><span style="color:#061324;">   private String </span><span style="color:#061324;">empName</span><span style="color:#061324;">;</span></p><p><span style="color:#061324;">   private </span><span style="color:#061324;">Dept</span><span style="color:#061324;">dept</span><span style="color:#061324;">;</span></p><p><span style="color:#061324;">}</span></p>
<class name="cn.jbit.hibernatedemo.entity.Emp" table="emp">
        <id name="empNo" column="EMPNO" type="java.lang.Integer">
            <generator class="assigned"/>
        </id>
			......
        <many-to-one name="dept" column="DEPTNO"       
              class="cn.jbit.hibernatedemo.entity.Dept"  />
</class>
<p><span style="color:#061324;">public class </span><span style="color:#061324;">Dept</span><span style="color:#061324;">implements </span><span style="color:#061324;">Serializable</span><span style="color:#061324;">{</span></p><p><span style="color:#061324;">   private Byte </span><span style="color:#061324;">deptNo</span><span style="color:#061324;">;</span></p><p><span style="color:#061324;">   private String </span><span style="color:#061324;">deptName</span><span style="color:#061324;">;</span></p><p><span style="color:#061324;">   private String location;</span></p><p><span style="color:#061324;">}</span></p>
<class name="cn.jbit.hibernatedemo.entity.Dept" table="dept">
        <id name="deptNo" column="DEPTNO" type="java.lang.Byte">
            <generator class="assigned"/>
        </id>
        <property name="deptName" type="java.lang.String" column="DNAME"/>
       ......
</class>



5.一对多双向关联关系

public class Dept implements Serializable {
    private Byte deptNo;
    private String deptName;
    private String location;
    private Set<Emp> emps = new HashSet<Emp>();
}
<p><span style="color:#061324;"><classname="cn.jbit.hibernatedemo.entity.Dept" table="dept"></span></p><p><span style="color:#061324;">       <id name="deptNo" column="DEPTNO"type="java.lang.Byte"></span></p><p><span style="color:#061324;">            <generatorclass="assigned"/></span></p><p><span style="color:#061324;">       </id></span></p><p><span style="color:#061324;">       ......</span></p><p><span style="color:#061324;">       <set name="emps"></span></p><p><span style="color:#061324;">         <keycolumn="DEPTNO"></key></span></p><p><span style="color:#061324;">         <one-to-manyclass="cn.jbit.hibernatedemo.entity.Emp"/></span></p><p><span style="color:#061324;">       </set></span></p><p><span style="color:#061324;"></class></span></p>


6.多对多关联关系

配置Project和Employee的多对多双向关联

Project Employee持久化类

Project.hbm.xmlEmployee.hbm.xml


<class name="cn.jbit.hibernatedemo.entity.Employee" table="EMPLOYEE">
    <id name="empid" type="java.lang.Integer">
        <column name="EMPID" precision="6" scale="0" />
        <generator class="assigned" />
    </id>
    ......
    <set name="projects" inverse="true" table="PROEMP">
        <key column="REMPID"/>
        <many-to-many class="cn.jbit.hibernatedemo.entity.Project" 
                                    column="RPROID" />
    </set>
</class>

<class name="cn.jbit.hibernatedemo.entity.Project" table="PROJECT">
    <id name="proid" type="java.lang.Integer">
        <column name="PROID" precision="6" scale="0" />
        <generator class="assigned" />
    </id>
    ......
    <set name="employees" table="PROEMP" cascade="save-update">
        <key column="RPROID" />
        <many-to-many class="cn.jbit.hibernatedemo.entity.Employee" 
                                    column="REMPID" />
    </set>
</class>





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27486次
    • 积分:898
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:17篇
    • 译文:0篇
    • 评论:0条
    文章分类