Hibernate的对象关系映射(5)

2.3 多态性

2.3.1 3种继承策略

(1) 共享一个数据表

利用子类之间识别字段来区分。

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.weportal.container" >
    
< class  name ="Container"  abstract ="true"  table ="CONTAINER"  where ="size &lt; 3000" >
        
< id  name ="containerId" >
            
< generator  class ="hilo" />
        
</ id >
        
< discriminator  column ="CONTAINER_TYPE"  type ="string" />
        
< property  name ="size" />
        
< property  name ="name" />         
        
< property  name ="description" />

        
< subclass  name ="Box"  discriminator-value ="BOX" >
            
< property  name ="height" >
            
</ property >
            
< property  name ="length" />
            
< property  name ="width" />                 
        
</ subclass >
        
        
< subclass  name ="Bottle"  discriminator-value ="BOTTLE" >
            
< property  name ="diameter" />
            
< property  name ="height" />
        
</ subclass >

    
</ class >
</ hibernate-mapping >

(2) 每个类一个数据表

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.weportal.container" >
  
< class  name ="Container"  table ="container_type2" >
      
< id  name ="containerId"  type ="string" >
          
< generator  class ="hilo" ></ generator >
      
</ id >
      
< property  name ="size" ></ property >
      
< property  name ="name" ></ property >
      
< property  name ="description" ></ property >
      
< joined-subclass  name ="Box"  table ="box_type2" >
          
< key  column ="containerId" ></ key >
          
< property  name ="height" ></ property >
          
< property  name ="length" ></ property >
          
< property  name ="width" ></ property >
      
</ joined-subclass >
      
< joined-subclass  name ="Bottle"  table ="bottle_type2" >
          
< key  column ="containerId" ></ key >
          
< property  name ="diameter" ></ property >
          
< property  name ="height" ></ property >
      
</ joined-subclass >
  
</ class >
</ hibernate-mapping >

(3) 每个子类一个数据表

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.weportal.subselect" >
    
< class  name ="Box"  table ="Box_type3" >
        
< id  name ="id" >
            
< generator  class ="hilo" />
        
</ id >
        
< property  name ="size"  column ="size" />
        
< property  name ="name"  column ="name" />         
        
< property  name ="description"  column ="description" />
        
< property  name ="width"  column ="width" />
        
< property  name ="length"  column ="length" />
        
< property  name ="height"  column ="height" />
    
</ class >
    
< class  name ="Bottle"  table ="Bottle_type3" >
        
< id  name ="id" >
            
< generator  class ="hilo" />
        
</ id >
        
< property  name ="size"  column ="size" />
        
< property  name ="name"  column ="name" />         
        
< property  name ="description"  column ="description" />
        
< property  name ="diameter"  column ="diameter" />     
        
< property  name ="height"  column ="height" />     
    
</ class >     
</ hibernate-mapping >

2.3.2 多重继承

可用第一种和第二种策略实现。

2.3.3 联合子类

在hibernate3.0中,除了使用subclass和joined-subclass支持多态外,还可以使用union-subclass来建立联合子类。

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.weportal.unionsubclass"  default-access ="field" >
    
< class  name ="Person" >
        
< id  name ="id" >
            
< generator  class ="hilo" />
        
</ id >
        
< property  name ="name" />         

        
< union-subclass  name ="Programmer" >
            
< property  name ="language" />
            
        
</ union-subclass >
        
        
< union-subclass  name ="Player" >
            
< property  name ="style" />
        
</ union-subclass >

    
</ class >
</ hibernate-mapping >

union-subclass元素也可以嵌套来支持多重继承关系。例如dbprogrammer extends programmer。

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.weportal.unionsubclass"  default-access ="field" >
    
< class  name ="Person" >
        
< id  name ="id" >
            
< generator  class ="hilo" />
        
</ id >
        
< property  name ="name" />         

        
< union-subclass  name ="Programmer" >
            
< property  name ="language" />
            
< union-subclass  name ="DBDesigner" >
                
< property  name ="platform" ></ property >
            
</ union-subclass >
        
</ union-subclass >
        
        
< union-subclass  name ="Player" >
            
< property  name ="style" />
        
</ union-subclass >

    
</ class >
</ hibernate-mapping >

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值