ITeye博客

Hibernate 各种映射用法

explore · 2010-05-17 21:50 · J2EE · 企业架构 · Markdown

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)

暂无评论。

评论功能维护中,当前仅展示历史评论。