Hibernate的映射关系很多,也比较复杂,基本上Hibernate的主要部分就是映射,这里把几个常用的映射介绍一下,以备自己查看。

一、首先是一对多,多对一关联映射,表模型就用公司表和员工表,它们的结构是公司表两个字段:公司编号(自增长),公司名称;员工表三个字段:员工编号(自增长),员工姓名,所属公司编号。

Company.java里面的代码,省略了get和set方法

 1 public class Company {
 2 
 3     Company() {
 4     }
 5 
 6     private Integer companyId;
 7     private String companyName;
 8     private Set<User> user;
 9        
10    ...
11 }

User.java里面的代码,省略get和set方法

 1 public class User {
 2 
 3     User() {
 4     }
 5 
 6     private Integer UserId;
 7     private String userName;
 8     private Company company;
 9 
10    ...
11 }

相关的Hibernate配置文件,Company.hbm.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping package="cn.dy.bean">
 5     <class name="Company" table="tb_company" catalog="learn">
 6         <id name="companyId" type="integer">
 7             <column name="companyId" />
 8             <generator class="identity" />
 9         </id>
10         <property name="companyName" type="string">
11             <column name="companyName" length="200" not-null="true">
12                 <comment>公司名称</comment>
13             </column>
14         </property>
15         <set name="user">
16             <key column="companyId" />
17             <one-to-many class="User" />
18         </set>
19     </class>
20 </hibernate-mapping>

User.hbm.xml代码为:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="cn.dy.bean.User" table="tb_user" catalog="learn">
 6         <id name="userId" type="integer">
 7             <column name="userId" />
 8             <generator class="identity" />
 9         </id>
10         <property name="userName" type="string">
11             <column name="userName" length="16" not-null="true">
12                 <comment>帐号</comment>
13             </column>
14         </property>
15        
16         <!-- 多对一,多个员工对应一个公司,员工表内companyId字段,对应公司表的主键companyId -->
17         <many-to-one name="company" column="companyId" not-null="true" />
18           </class>
19 </hibernate-mapping>

对应这4段代码,需要注意的是:

一对多配置
1.Company.java里面加入属性Set<User>;
2.Company.hbm.xml文件里面加上set配置,详细见上面代码;
多对一配置
3.User.java里面要加入复杂属性Company;
4.User.java里面不能出现属性companyId;
5.User.hbm.xml文件加上<many-to-one>配置,column为多的那一方表的一个字段,这里是companyId,默认对应公司表的主键。

二、一对一映射,员工表和身份证号码表(编号字段,和员工表员工编号一致;身份证号码字段)

 1 public class User {
 2 
 3     User() {
 4     }
 5 
 6     private Integer userId;
 7     private String userName;
 8     private IdCard idCard;
 9 
10    ...
11 }

IdCard.java代码:

 1 public class IdCard{
 2 
 3     IdCard() {
 4     }
 5 
 6     private Integer id;
 7     private String cardNum;
 8     private User user;
 9 
10   ...
11 }

User.hbm.xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="cn.dy.bean.User" table="tb_user" catalog="learn">
 6         <id name="userId" type="integer">
 7             <column name="userId" />
 8             <generator class="identity" />
 9         </id>
10         <property name="userName" type="string">
11             <column name="userName" length="16" not-null="true">
12                 <comment>帐号</comment>
13             </column>
14         </property>
15       <one-to-one name="idCard" />
16           </class>
17 </hibernate-mapping>

IdCard.hbm.xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="cn.dy.bean.IdCard" table="tb_idCard" catalog="learn">
 6         <id name="id">
 7             <generator class="foreign">
 8                 <param name="property">user</param>
 9             </generator>
10         </id>
11         <property name="cardNum" type="string">
12             <column name="cardName" length="18" not-null="true">
13                 <comment>身份证号码</comment>
14             </column>
15         </property>
16        <one-to-one name="user" />
17     </class>
18 </hibernate-mapping>

三、摘编的其他网站信息:(技术是让使用的,而不是一种桎梏,所以要灵活利用,不能说我使用Hibernate就必须用映射,有时候还要考虑效率问题,具体自己把握吧)

Hibernate 一对一"连接表"双向关联 
<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
        <join table="join" optional="true"> 
         <key column="personId"  unique="true"/> 
         <many-to-one name="address" column="addressId" not-null="true" unique="true"/>
        </join> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressDetail"/> 
        <join table="join" optional="true" inverse="true"> 
          <key column="addressId" unique="true"/> 
          <many-to-one name="person" column="personId" not-null="true" unique="true"/> 
        </join> 
    </class> 
</hibernate-mapping> 

Hibernate 一对一"主键"双向关联 
<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
        <one-to-one name="address" cascade="all"/> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="foreign"> 
                <param name="property">person</param> 
            </generator> 
        </id> 
        <property name="addressDetail"/> 
        <!--表明主键由关联属性生成--> 
        <one-to-one name="person" cascade="all" constrained="true"/> 
    </class> 
</hibernate-mapping> 
Hibernate 一对一"外键"双向关联   
<hibernate-mapping>   
    <class name="cn.dy.Person" table="PERSON">   
        <id name="personId">   
            <generator class="identity"/>   
        </id>   
        <property name="name"/>   
        <property name="age"/>   
        <one-to-one name="address" cascade="all" property-ref="person"/>   
    </class>   
</hibernate-mapping>    
<hibernate-mapping>   
    <class name="cn.dy.Address" table="ADDRESS">   
        <id name="addressId">   
            <generator class="identity"/>   
        </id>   
        <property name="addressDetail"/>   
        <many-to-one name="person" unique="true"/>   
    </class>   
</hibernate-mapping>   

Hibernate 多对多单向关联 

<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
        <!--映射集合属性,join是连接表表名--> 
        <set name="addresses" table="join" > 
           <key column="personId"/> 
             <many-to-many  column="addressId" class="cn.dy.Address"/> 
        </set> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressDetail"/> 
    </class> 
</hibernate-mapping> 
Hibernate 多对一"连接表"单向关联 

<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
            <join table="join"> 
             <key column="personId"/> 
            <many-to-one name="address"/> 
        </join> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressDetail"/> 
    </class> 
</hibernate-mapping> 

Hibernate 多对一外键单向关联 

<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
        <many-to-one name="address" column="addressId"/> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressDetail"/> 
    </class> 
</hibernate-mapping> 

Hibernate 一对多连接表单向关联 

<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
           <set name="addresses" table="join" > 
            <key column="personId"/> 
            <many-to-many column="addressId" unique="true" class="cn.dy.Address"/> 
        </set> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressDetail"/> 
    </class> 
</hibernate-mapping> 
Hibernate 一对一主键单向关联 

<id name="personId"> 
<generator class="foreign"> 
<param name="property">address</param> 
</generator> 
</id> 
<one-to-one name="address" constrained="true"/> 
<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personId" column="presonid"> 
         <generator class="foreign"> 
<param name="property">address</param> 
            </generator> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
  <one-to-one name="address" constrained="true"/> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressId"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressDetail"/> 
    </class> 
</hibernate-mapping> 

Hibernate 一对一外键单向关联

<many-to-one name="address" column="addressId" unique="true"/>
<hibernate-mapping>
    <class name="cn.dy.Address" table="ADDRESS">
        <id name="addressId">
            <generator class="identity"/>
        </id>
        <property name="addressDetail"/>
    </class>
</hibernate-mapping>
<hibernate-mapping>
    <class name="cn.dy.Person" table="PERSON">
        <id name="personId">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <many-to-one name="address" column="addressId" unique="true"/>
    </class>
</hibernate-mapping>

Hibernate 一对多外键单向关联 

 <set name="addresses"  table="ADDRESS"  cascade="all" > 
       <key column="personid"/> 
       <one-to-many class="cn.dy..Address"/> 
 </set> 
<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personid"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
        <set name="addresses"  table="ADDRESS" cascade="all" > 
            <key column="personid"/> 
            <one-to-many class="cn.dy..Address"/> 
        </set> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressid"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressdetail"/> 
    </class> 
</hibernate-mapping>  

Hibernate 一对一连接表单向关联  

<join table="join"> 
<key column="personid"/> 
<many-to-one name="address" unique="true"/> 
</join> 
<hibernate-mapping> 
    <class name="cn.dy.Person" table="PERSON"> 
        <id name="personid"> 
            <generator class="identity"/> 
        </id> 
        <property name="name"/> 
        <property name="age"/> 
            <join table="join"> 
            <key column="personid"/> 
            <many-to-one name="address" unique="true"/> 
        </join> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping> 
    <class name="cn.dy.Address" table="ADDRESS"> 
        <id name="addressid"> 
            <generator class="identity"/> 
        </id> 
        <property name="addressdetail"/> 
    </class> 
</hibernate-mapping>  



      此文部分内容来源网络。如有侵犯您的版权问题,请来消息至电子邮件DyEngima&163.com(&换成@),经核实后会在文章内部标明来源。
转载请注明来源http://www.blogjava.net/DyEnigma/
签名:有能力、有担当、有情义的人才能称之为男人,而不是由性别决定。