Hibernate 各种映射用法

1. 值集合映射:这些集合包含的元素不是Domain Object,是一些值对象

  1.1 Set映射:
        <set name="Set属性名" table="表名">
            <key column="与主键关联字段名" />
            <element type="数据类型" column="字段名" />
        </set>
  1.2 List映射:
        <list name="List属性名" table="表名">
            <key column="与主键关联字段名" />
            <list-index column="索引字段名" />
            <element type="数据类型" column="字段名" />
        </list>
  1.3Map映射:
        <map name="Map属性名" table="表名">
            <key column="与主键关联字段名" />
            <map-key type="数据类型" column="KEY字段名" />
            <element type="数据类型" column="VALUE字段名" />
        </map>

  1.4 Bag映射:
        <bag name="bag属性名" table="表名">
            <key column="与主键关联字段名" />        
            <element type="数据类型" column="字段名" />
        </bag>
  1.5 Idbag映射:
        <idbag name="idbag 属性名" table="表名"> 

            <collection-id column="cid" type="java.lang.String">

                    <generator class="uuid.hex" />

             </collection-id>

            <key column="与主键关联字段名" />
            <element type="数据类型" column="字段名" />
        </idbag>

Bag和Set一样是无序集合,和Set不同的是,Bag允许元素重复,可以将List映射为Bag。

Bag与IdBag的区别:

public class User {
      private List<String> emails=new ArrayList<String>();

      public void addEmail(String email) {   
           emails.add(email);    
      }         

      public void removeEmail(String email) {      
            emails.remove(email);   
     }

CREATE TABLE user (
    id INT(11) NOT NULL auto_increment PRIMARY KEY
);

CREATE TABLE emails(
    userId INT(11) NOT NULL,
    email  VARCHAR(100) NOT NULL
);

<bag name="emails" table="emails">    
        <key column="userId" />    
        <element column="email" type="java.lang.String" />    
 </bag> 

<idbag name="emails" table="emails">     
    <collection-id column="cid" type="java.lang.String">                   
          <generator class="uuid.hex" />       
   </collection-id>        
   <key column="email" />
   <element column="email" type="java.lang.String" />    
</idbag>

User user1=new User();
user1.addEmail("email1@139.com");
user1.addEmail("email2@139.com");
user1.addEmail("email2@139.com");

User user2=new User();
user2.addEmail("email3@139.com");

//save user

//user table
      id
      1
      2

 

 如果采用bag,则emails表如下:

userid         email
    1             email1
    1             email2
    1             email2
    2             email3

执行user1.remove(email2),根据List的remove方法,会删除第一个匹配的元素,但是Hibernate无法定位此元素,因而采取的方式为先全部清空,再重新插入数据库,效率低下,生成的sql意思如下:

delete from emails where userId=1;

insert into emails(userId,email) values(1,email1);

insert into emails(userId,email) values(1,email2);

insert into emails(userId,email) values(2,email3);

 

如果采用Idbag,则emails表如下:

 cid         userid         email
 cid1          1             email1
 cid2          1             email2
 cid3          1             email2
 cid4          2             email3

执行user1.remove(email2),生成的sql意思如下:

delete from emails where cid=cid2;

此方式效率较高。

 

2. 实体关系映射

 <many-to-one name="company" column="companyid" class="com.ijo.domain.user.Company">

 

  <set name="subProjects" table="subproject_programmer"  inverse="true">
        <key column="programmerid"/>
        <many-to-many class="com.ijo.domain.project.SubProject" column="subprojectid"/>
  </set>


  <bag name="subscriptions" table="subscription" >
         <key column="companyid"/>
         <one-to-many class="com.ijo.domain.subscription.Subscription"/>
   </bag>

 3. 继承关系映射

3.1 <subclass name="com.ijo.domain.common.category.AdjustCategory" discriminator -  

           value="AdjustCategory">
      </subclass>
      <subclass name="com.ijo.domain.common.category.TransportCategory" discriminator- 

            value="TransportCategory">
      </subclass>

 

3.2 <joined-subclass name="com.ijo.domain.user.CustomerUser" table="customeruser">
            <key column="userid"/>
       </joined-subclass>

 



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值