Hibernate继承映射--每个类一个数据表的实现案例

首先,我们有如下实体关系,Container,Box,Bottle,Container是Box和Bottle的父类

数据库结构如下:
其中hibernate_unique_key表为hibernate主键生成策略的hilo高低位算法使用

CREATE   TABLE  `box1` (
  `id` 
varchar ( 50 NOT   NULL   default   ' 0 ' ,
  `size` 
double   default   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  `description` 
varchar ( 50 default   NULL ,
  `width` 
double   default   NULL ,
  `height` 
double   default   NULL ,
  `
len double   default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

CREATE   TABLE  `container1` (
  `id` 
varchar ( 50 NOT   NULL   default   ' 0 ' ,
  `size` 
double   default   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  `description` 
varchar ( 50 default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

CREATE   TABLE  `bottle1` (
  `id` 
varchar ( 50 NOT   NULL   default   ' 0 ' ,
  `size` 
double   default   NULL ,
  `name` 
varchar ( 20 default   NULL ,
  `diameter` 
double   default   NULL ,
  `height` 
double   default   NULL ,
  `description` 
varchar ( 50 default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

CREATE   TABLE  `hibernate_unique_key` (
  `next_hi` 
int ( 11 NOT   NULL   default   ' 0 ' ,
  
PRIMARY   KEY   (`next_hi`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

 

实体定义如下:

package  extendstestone;
// hibernate多态支持的策略,每个子类一张表
public   abstract   class  Container  ... {
  
private String containerId;
  
private double size;
  
private String name;
  
private String description;
public String getContainerId() ...{
    
return containerId;
}

public void setContainerId(String containerId) ...{
    
this.containerId = containerId;
}

public String getDescription() ...{
    
return description;
}

public void setDescription(String description) ...{
    
this.description = description;
}

public String getName() ...{
    
return name;
}

public void setName(String name) ...{
    
this.name = name;
}

public double getSize() ...{
    
return size;
}

public void setSize(double size) ...{
    
this.size = size;
}

}



package  extendstestone;

public   class  Box  extends  Container  ... {
    
private double height;
    
private double length;
    
private double width;
    
public double getHeight() ...{
        
return height;
    }

    
public void setHeight(double height) ...{
        
this.height = height;
    }

    
public double getLength() ...{
        
return length;
    }

    
public void setLength(double length) ...{
        
this.length = length;
    }

    
public double getWidth() ...{
        
return width;
    }

    
public void setWidth(double width) ...{
        
this.width = width;
    }

}



package  extendstestone;

public   class  Bottle  extends  Container  ... {
  
private double diameter;
  
private double height;
public double getDiameter() ...{
    
return diameter;
}

public void setDiameter(double diameter) ...{
    
this.diameter = diameter;
}

public double getHeight() ...{
    
return height;
}

public void setHeight(double height) ...{
    
this.height = height;
}

}

 配置文件:

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="extendstestone" >
    
< class  name ="Container"  table ="container1" >
       
< id  name ="containerId"  column ="id" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >
       
< property  name ="size"  column ="size" ></ property >
       
< property  name ="name"  column ="name" ></ property >
       
< property  name ="description"  column ="description" ></ property >
       
< joined-subclass  name ="extendstestone.Box"  table ="box1" >
          
< key  column ="id" ></ key >
          
< property  name ="height"  column ="height" ></ property >
          
< property  name ="width"  column ="width" ></ property >
          
< property  name ="length"  column ="len" ></ property >
       
</ joined-subclass >
         
< joined-subclass  name ="extendstestone.Bottle"  table ="bottle1" >
          
< key  column ="id" ></ key >
          
< property 

你可能感兴趣的:(数据结构,算法,.net,Hibernate,MyEclipse)