首先,在数据库中建立表:
    
    CREATE TABLE `container` (
    
      `containerId` varchar(50) NOT NULL,
    
      `container_type` varchar(50) NOT NULL,
    
      `size` double default NULL,
    
      `name` varchar(50) default NULL,
    
      `description` varchar(50) default NULL,
    
      `height` double default NULL,
    
      `length` double default NULL,
    
      `width` double default NULL,
    
      `diameter` double default NULL,
    
      PRIMARY KEY  (`containerId`)
    
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
  
其中Container是一个抽象类,其下有两个子类Box和Bottle
 package
      
      
         abstracttest;
      
        package
      
      
         abstracttest;
         
         public
      
      
         
      
      
        abstract
      
      
         
      
      
        class
      
      
         Container 
      
      
      
        
          {
        
      
      
        public
      
      
         
      
      
        abstract
      
      
         
      
      
        class
      
      
         Container 
      
      
      
        
          {
           private
        
        
           String containerId;
            
        
        
          private
        
        
           String containerId;
           private
        
        
           
        
        
          double
        
        
           size;
            
        
        
          private
        
        
           
        
        
          double
        
        
           size;
           private
        
        
           String name;
            
        
        
          private
        
        
           String name;
           private
        
        
           String description;
            
        
        
          private
        
        
           String description;
           public
        
        
           String getContainerId() 
        
        
        
          
            {
          
        
        
          public
        
        
           String getContainerId() 
        
        
        
          
            {
             return
          
          
             containerId;
                
          
          
            return
          
          
             containerId;
             }
            }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setContainerId(String containerId) 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          void
        
        
           setContainerId(String containerId) 
        
        
        
          
            {
             this
          
          
            .containerId 
          
          
            =
          
          
             containerId;
                
          
          
            this
          
          
            .containerId 
          
          
            =
          
          
             containerId;
             }
            }
          
        
        
           public
        
        
           String getDescription() 
        
        
        
          
            {
          
        
        
          public
        
        
           String getDescription() 
        
        
        
          
            {
             return
          
          
             description;
                
          
          
            return
          
          
             description;
             }
            }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setDescription(String description) 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          void
        
        
           setDescription(String description) 
        
        
        
          
            {
             this
          
          
            .description 
          
          
            =
          
          
             description;
                
          
          
            this
          
          
            .description 
          
          
            =
          
          
             description;
             }
            }
          
        
        
           public
        
        
           String getName() 
        
        
        
          
            {
          
        
        
          public
        
        
           String getName() 
        
        
        
          
            {
             return
          
          
             name;
                
          
          
            return
          
          
             name;
             }
            }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setName(String name) 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          void
        
        
           setName(String name) 
        
        
        
          
            {
             this
          
          
            .name 
          
          
            =
          
          
             name;
                
          
          
            this
          
          
            .name 
          
          
            =
          
          
             name;
             }
            }
          
        
        
           public
        
        
           
        
        
          double
        
        
           getSize() 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          double
        
        
           getSize() 
        
        
        
          
            {
             return
          
          
             size;
                
          
          
            return
          
          
             size;
             }
            }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setSize(
        
        
          double
        
        
           size) 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          void
        
        
           setSize(
        
        
          double
        
        
           size) 
        
        
        
          
            {
             this
          
          
            .size 
          
          
            =
          
          
             size;
                
          
          
            this
          
          
            .size 
          
          
            =
          
          
             size;
             }
            }
          
        
        
           }
          }
        
      
      
         
      
    
 package
      
      
         abstracttest;
      
        package
      
      
         abstracttest;
         
         public
      
      
         
      
      
        class
      
      
         Bottle 
      
      
        extends
      
      
         Container 
      
      
      
        
          {
        
      
      
        public
      
      
         
      
      
        class
      
      
         Bottle 
      
      
        extends
      
      
         Container 
      
      
      
        
          {
           private
        
        
           
        
        
          double
        
        
           diameter;
            
        
        
          private
        
        
           
        
        
          double
        
        
           diameter;
           private
        
        
           
        
        
          double
        
        
           height;
            
        
        
          private
        
        
           
        
        
          double
        
        
           height;
           public
        
        
           
        
        
          double
        
        
           getDiameter() 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          double
        
        
           getDiameter() 
        
        
        
          
            {
             return
          
          
             diameter;
                
          
          
            return
          
          
             diameter;
             }
            }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setDiameter(
        
        
          double
        
        
           diameter) 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          void
        
        
           setDiameter(
        
        
          double
        
        
           diameter) 
        
        
        
          
            {
             this
          
          
            .diameter 
          
          
            =
          
          
             diameter;
                
          
          
            this
          
          
            .diameter 
          
          
            =
          
          
             diameter;
             }
            }
          
        
        
           public
        
        
           
        
        
          double
        
        
           getHeight() 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          double
        
        
           getHeight() 
        
        
        
          
            {
             return
          
          
             height;
                
          
          
            return
          
          
             height;
             }
            }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setHeight(
        
        
          double
        
        
           height) 
        
        
        
          
            {
          
        
        
          public
        
        
           
        
        
          void
        
        
           setHeight(
        
        
          double
        
        
           height) 
        
        
        
          
            {
             this
          
          
            .height 
          
          
            =
          
          
             height;
                
          
          
            this
          
          
            .height 
          
          
            =
          
          
             height;
             }
            }
          
        
        
           }
          }
        
      
      
         
      
    
 package
      
      
         abstracttest;
      
        package
      
      
         abstracttest;
         
         public
      
      
         
      
      
        class
      
      
         Box 
      
      
        extends
      
      
         Container 
      
      
      
        
          {
        
      
      
        public
      
      
         
      
      
        class
      
      
         Box 
      
      
        extends
      
      
         Container 
      
      
      
        
          {
           private
        
        
           
        
        
          double
        
        
           height;
              
        
        
          private
        
        
           
        
        
          double
        
        
           height;
           private
        
        
           
        
        
          double
        
        
           length;
              
        
        
          private
        
        
           
        
        
          double
        
        
           length;
           private
        
        
           
        
        
          double
        
        
           width;
              
        
        
          private
        
        
           
        
        
          double
        
        
           width;
           public
        
        
           
        
        
          double
        
        
           getHeight() 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          double
        
        
           getHeight() 
        
        
        
          
            {
             return
          
          
             height;
                    
          
          
            return
          
          
             height;
             }
                }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setHeight(
        
        
          double
        
        
           height) 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          void
        
        
           setHeight(
        
        
          double
        
        
           height) 
        
        
        
          
            {
             this
          
          
            .height 
          
          
            =
          
          
             height;
                    
          
          
            this
          
          
            .height 
          
          
            =
          
          
             height;
             }
                }
          
        
        
           public
        
        
           
        
        
          double
        
        
           getLength() 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          double
        
        
           getLength() 
        
        
        
          
            {
             return
          
          
             length;
                    
          
          
            return
          
          
             length;
             }
                }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setLength(
        
        
          double
        
        
           length) 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          void
        
        
           setLength(
        
        
          double
        
        
           length) 
        
        
        
          
            {
             this
          
          
            .length 
          
          
            =
          
          
             length;
                    
          
          
            this
          
          
            .length 
          
          
            =
          
          
             length;
             }
                }
          
        
        
           public
        
        
           
        
        
          double
        
        
           getWidth() 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          double
        
        
           getWidth() 
        
        
        
          
            {
             return
          
          
             width;
                    
          
          
            return
          
          
             width;
             }
                }
          
        
        
           public
        
        
           
        
        
          void
        
        
           setWidth(
        
        
          double
        
        
           width) 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          void
        
        
           setWidth(
        
        
          double
        
        
           width) 
        
        
        
          
            {
             this
          
          
            .width 
          
          
            =
          
          
             width;
                    
          
          
            this
          
          
            .width 
          
          
            =
          
          
             width;
             }
                }
          
        
        
           }
          }
        
      
      
         
      
    
在HBM文件中,定义,抽象类的配置
 <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
         <!
      
      
        DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      
      
        <!
      
      
        DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
      
      
        >
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
      
      
        >
      
      
         <!--
      
      
        <!--
      
      
         
         Mapping file autogenerated by MyEclipse - Hibernate Tools
            Mapping file autogenerated by MyEclipse - Hibernate Tools
         -->
      
      
        -->
      
      
         <
      
      
        hibernate-mapping 
      
      
        package
      
      
        ="abstracttest"
      
      
        >
      
      
        <
      
      
        hibernate-mapping 
      
      
        package
      
      
        ="abstracttest"
      
      
        >
      
      
         <
      
      
        class 
      
      
        name
      
      
        ="Container"
      
      
         abstract
      
      
        ="true"
      
      
         table
      
      
        ="container"
      
      
        >
            
      
      
        <
      
      
        class 
      
      
        name
      
      
        ="Container"
      
      
         abstract
      
      
        ="true"
      
      
         table
      
      
        ="container"
      
      
        >
      
      
         <
      
      
        id 
      
      
        name
      
      
        ="containerId"
      
      
        >
               
      
      
        <
      
      
        id 
      
      
        name
      
      
        ="containerId"
      
      
        >
      
      
         <
      
      
        generator 
      
      
        class
      
      
        ="uuid.hex"
      
      
        ></
      
      
        generator
      
      
        >
                 
      
      
        <
      
      
        generator 
      
      
        class
      
      
        ="uuid.hex"
      
      
        ></
      
      
        generator
      
      
        >
      
      
         </
      
      
        id
      
      
        >
               
      
      
        </
      
      
        id
      
      
        >
      
      
         <
      
      
        discriminator 
      
      
        column
      
      
        ="container_type"
      
      
        ></
      
      
        discriminator
      
      
        >
               
      
      
        <
      
      
        discriminator 
      
      
        column
      
      
        ="container_type"
      
      
        ></
      
      
        discriminator
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="size"
      
      
         column
      
      
        ="size"
      
      
        ></
      
      
        property
      
      
        >
               
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="size"
      
      
         column
      
      
        ="size"
      
      
        ></
      
      
        property
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="name"
      
      
         column
      
      
        ="name"
      
      
        ></
      
      
        property
      
      
        >
               
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="name"
      
      
         column
      
      
        ="name"
      
      
        ></
      
      
        property
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="description"
      
      
         column
      
      
        ="description"
      
      
        ></
      
      
        property
      
      
        >
               
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="description"
      
      
         column
      
      
        ="description"
      
      
        ></
      
      
        property
      
      
        >
      
      
         <
      
      
        subclass 
      
      
        name
      
      
        ="Box"
      
      
         discriminator-value
      
      
        ="BOX"
      
      
        >
               
      
      
        <
      
      
        subclass 
      
      
        name
      
      
        ="Box"
      
      
         discriminator-value
      
      
        ="BOX"
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="height"
      
      
         column
      
      
        ="height"
      
      
        ></
      
      
        property
      
      
        >
                 
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="height"
      
      
         column
      
      
        ="height"
      
      
        ></
      
      
        property
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="width"
      
      
         column
      
      
        ="width"
      
      
        ></
      
      
        property
      
      
        >
                 
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="width"
      
      
         column
      
      
        ="width"
      
      
        ></
      
      
        property
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="length"
      
      
         column
      
      
        ="length"
      
      
        ></
      
      
        property
      
      
        >
                 
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="length"
      
      
         column
      
      
        ="length"
      
      
        ></
      
      
        property
      
      
        >
      
      
         </
      
      
        subclass
      
      
        >
               
      
      
        </
      
      
        subclass
      
      
        >
      
      
         <
      
      
        subclass 
      
      
        name
      
      
        ="Bottle"
      
      
         discriminator-value
      
      
        ="BOTTLE"
      
      
        >
               
      
      
        <
      
      
        subclass 
      
      
        name
      
      
        ="Bottle"
      
      
         discriminator-value
      
      
        ="BOTTLE"
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="diameter"
      
      
         column
      
      
        ="diameter"
      
      
        ></
      
      
        property
      
      
        >
                 
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="diameter"
      
      
         column
      
      
        ="diameter"
      
      
        ></
      
      
        property
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="height"
      
      
         column
      
      
        ="height"
      
      
        ></
      
      
        property
      
      
        >
                 
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="height"
      
      
         column
      
      
        ="height"
      
      
        ></
      
      
        property
      
      
        >
      
      
         </
      
      
        subclass
      
      
        >
               
      
      
        </
      
      
        subclass
      
      
        >
      
      
         </
      
      
        class
      
      
        >
            
      
      
        </
      
      
        class
      
      
        >
      
      
         </
      
      
        hibernate-mapping
      
      
        >
      
      
        </
      
      
        hibernate-mapping
      
      
        >
      
      
         
      
    
    最后是测试代码
    
  
 package
      
      
         abstracttest;
      
        package
      
      
         abstracttest;
         
         import
      
      
         org.hibernate.Session;
      
      
        import
      
      
         org.hibernate.Session;
         import
      
      
         org.hibernate.SessionFactory;
      
      
        import
      
      
         org.hibernate.SessionFactory;
         import
      
      
         org.hibernate.Transaction;
      
      
        import
      
      
         org.hibernate.Transaction;
         import
      
      
         org.hibernate.cfg.Configuration;
      
      
        import
      
      
         org.hibernate.cfg.Configuration;
         
         
         public
      
      
         
      
      
        class
      
      
         Test 
      
      
      
        
          {
        
      
      
        public
      
      
         
      
      
        class
      
      
         Test 
      
      
      
        
          {
           
           /**
          
              
        
        
        
          
            /**
          
          
             * 
          
          
            @param
          
          
             args
                 * 
          
          
            @param
          
          
             args
             */
                 
          
          
            */
          
        
        
           public
        
        
           
        
        
          static
        
        
           
        
        
          void
        
        
           main(String[] args) 
        
        
        
          
            {
          
              
        
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          void
        
        
           main(String[] args) 
        
        
        
          
            {
             Configuration cfg
          
          
            =
          
          
            new
          
          
             Configuration();
                    Configuration cfg
          
          
            =
          
          
            new
          
          
             Configuration();
             cfg.configure();
                    cfg.configure();
             SessionFactory sf
          
          
            =
          
          
            cfg.buildSessionFactory();
                    SessionFactory sf
          
          
            =
          
          
            cfg.buildSessionFactory();
             Session session
          
          
            =
          
          
            sf.openSession();
                    Session session
          
          
            =
          
          
            sf.openSession();
             Transaction t
          
          
            =
          
          
            session.beginTransaction();
                    Transaction t
          
          
            =
          
          
            session.beginTransaction();
             Box box1
          
          
            =
          
          
            new
          
          
             Box();
                    Box box1
          
          
            =
          
          
            new
          
          
             Box();
             box1.setName(
          
          
            "
          
          
            木箱子
          
          
            "
          
          
            );
                    box1.setName(
          
          
            "
          
          
            木箱子
          
          
            "
          
          
            );
             box1.setHeight(
          
          
            50
          
          
            );
                    box1.setHeight(
          
          
            50
          
          
            );
             box1.setLength(
          
          
            50
          
          
            );
                    box1.setLength(
          
          
            50
          
          
            );
             box1.setWidth(
          
          
            50
          
          
            );
                    box1.setWidth(
          
          
            50
          
          
            );
             box1.setDescription(
          
          
            "
          
          
            包装电视机
          
          
            "
          
          
            );
                    box1.setDescription(
          
          
            "
          
          
            包装电视机
          
          
            "
          
          
            );
             box1.setSize(
          
          
            12500
          
          
            );
                    box1.setSize(
          
          
            12500
          
          
            );
             
                    
             Bottle bottle1
          
          
            =
          
          
            new
          
          
             Bottle();
                    Bottle bottle1
          
          
            =
          
          
            new
          
          
             Bottle();
             bottle1.setName(
          
          
            "
          
          
            玻璃瓶子
          
          
            "
          
          
            );
                    bottle1.setName(
          
          
            "
          
          
            玻璃瓶子
          
          
            "
          
          
            );
             bottle1.setDescription(
          
          
            "
          
          
            喝牛奶用
          
          
            "
          
          
            );
                    bottle1.setDescription(
          
          
            "
          
          
            喝牛奶用
          
          
            "
          
          
            );
             bottle1.setSize(
          
          
            139.12
          
          
            );
                    bottle1.setSize(
          
          
            139.12
          
          
            );
             bottle1.setDiameter(
          
          
            6
          
          
            );
                    bottle1.setDiameter(
          
          
            6
          
          
            );
             bottle1.setHeight(
          
          
            12
          
          
            );
                    bottle1.setHeight(
          
          
            12
          
          
            );
             
                    
             sessio
                    sessio
          
        
      
    


 
       
         
					 
					