Hibernate继承关系的实体设计的两种方法

 
1 对于父类和子类分别对应数据库中的一个表,父类表中的字段是公共的属性,子类表中是特殊的属性。
数据库表:
父表: titem 字段:id (id) name (名称) manufacturer (产地)
               子表1:ibook 字段:id (id) pagecount(页数)
         子表2:idvd  字段:id (id)  regioncode (编号)
(1)    创建数据库:
-- 删除数据库表
drop table Titem;
drop table TBook;
drop table TDVD;
-- 创建数据库表
create table Titem(
 id varchar (32) not null primary key ,
  name varchar (20) not null ,
 manufacturer varchar ()
);
create table TBook(
 id varchar (32) not null primary key ,
 pagecount int
);
create table TDvd(
 id varchar (32) not null primary key ,
 regioncode varchar (2)
);
2 )编写类文件:
TItem 类:
package hibernate.hbu;
public class TItem {
 
    private String id ;
 
    private String name ;
 
    private String manufacturer ;
 
    public String getId() {
       return id ;
    }
 
    public void setId(String id) {
       this . id = id;
    }
 
    public String getManufacturer() {
       return manufacturer ;
    }
 
    public void setManufacturer(String manufacturer) {
       this . manufacturer = manufacturer;
    }
 
    public String getName() {
       return name ;
    }
 
    public void setName(String name) {
       this . name = name;
    }
}
TBook 类:
package hibernate.hbu;
 
public class TBook extends TItem {
 
    private int pagecount ;
 
    public int getPagecount() {
       return pagecount ;
    }
 
    public void setPagecount( int pagecount) {
       this . pagecount = pagecount;
    }
 
}
 
TDvd 类:
package hibernate.hbu;
 
public class TDvd extends TItem {
 
    private String regioncode ;
 
    public String getRegioncode() {
       return regioncode ;
    }
 
    public void setRegioncode(String regioncode) {
       this . regioncode = regioncode;
    }
}
3 )映射文件: titem.hbm.xml
<? 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 Persistence Tools
-->
< hibernate-mapping >
    < class name = "hibernate.zyl.TItem" table = "titem" catalog = "test" >
       < id name = "id" type = "java.lang.String" >
           < column name = "id" length = "32" />
           < generator class = "assigned" />
       </ id >
       < property name = "name" type = "java.lang.String" >
           < column name = "name" length = "20" not-null = "true" />
       </ property >
       < property name = "manufacturer" type = "java.lang.String" >
           < column name = "manufacturer" length = "255" />
       </ property >
       <joined-subclass name="hibernate.zyl.TBook" table="tbook">
           <key column="id"></key>
           <property name="pagecount" type="java.lang.Integer"
              column="pagecount">
           </property>
       </joined-subclass>
       <joined-subclass name="hibernate.zyl.TDvd" table="tdvd">
           <key column="id"></key>
           <property name="regioncode" type="java.lang.String"
              column="regioncode">
           </property>
       </joined-subclass>
    </ class >
</ hibernate-mapping >
3 )测试类: Test.java
     package hibernate.zyl;
 
import hibernate.sessionFactory.HibernateSessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
 
    /**
      * @param args
      */
    public static void main(String[] args) {
       // TODO Auto-generated method stub
 
       Session session = HibernateSessionFactory.getSession();
       Transaction tr = null ;
 
       // TDvd dvd = new TDvd();
       // dvd.setId("001");
       // dvd.setRegioncode("0");
       // dvd.setManufacturer("hebei");
       // dvd.setName("net");
       // dvd.setRegioncode("6");
 
       TBook book = new TBook();
       book.setId( "005" );
       book.setName( "Java" );
       book.setManufacturer( " 机械工业 " );
       book.setPagecount(66);
 
       tr = session.beginTransaction();
       // session.save(dvd);
       session.save(book);
       tr.commit();
    }
}
 
2 在一个表中实现:
表: Titem  字段: id  name  category (特殊的标志位) manufacturer (产地)
 pagecode (页数)  regioncode(编号 )
  category 的值是 1 时,我们让此条记录指向一个 TBook 类,其中 pagecode 是具体的值,而 regioncode 的值是空的;当 category 的值是 2 时,我们让此条记录指向一个 TDvd 类,其中 pagecode 是空的,而 regioncode 的值是具体的值
 
(1)       创建数据库:
  -- 删除数据库表
drop table Titem;
-- 创建数据库表
create table Titem(
 id varchar (32) not null primary key ,
  name varchar (20) not null ,
 category varchar (50) not null ,
 manufacturer varchar (50),
 regioncode varchar (2),
 pagecount int
);
(2)       编写类文件:
  类文件的内容和前面的相同,不过要指出:虽然在表中增加了一个 category 字段但我们并不在类文件中增加这个属性,只是在映射文件中加以配置。
3 )映射文件: titem.hbm.xml
<? 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 Persistence Tools
-->
< hibernate-mapping >
    < class name = "hibernate.zyl.TItem" table = "titem" catalog = "test" >
       < id name = "id" type = "java.lang.String" >
           < column name = "id" length = "32" />
           < generator class = "assigned" />
       </ id >
       <discriminator column="category" type="java.lang.String"
           not-null="true" length="50">
       </ discriminator >
       < property name = "name" type = "java.lang.String" >
           < column name = "name" length = "20" not-null = "true" />
       </ property >
       < property name = "manufacturer" type = "java.lang.String" >
           < column name = "manufacturer" length = "50" />
       </ property >
        <subclass name="hibernate.zyl.TBook" discriminator-value="1">
           <property name="pagecount" column="pagecount"
              type="java.lang.Integer">
           </property>
       </subclass>
       <subclass name="hibernate.zyl.TDvd" discriminator-value="2">
           <property name="regioncode" column="regioncode"
              type="java.lang.String">
           </property>
       </subclass>
    </ class >
</ hibernate-mapping >
3 )测试类: Test.java
    package hibernate.zyl;
 
import java.util.Iterator;
 
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import sessionFactory.HibernateSessionFactory;
 
public class Test {
 
    /**
      * @param args
      */
    public static void main(String[] args) {
       // TODO Auto-generated method stub
 
       Session session = HibernateSessionFactory.getSession();
       Transaction tr = null ;
 
       // TBook book = new TBook();
       // book.setId("009");
       // book.setName("Java");
       // book.setManufacturer(" 石家庄 ");
       // book.setPagecount(2);
 
       // TDvd dvd = new TDvd();
       // dvd.setId("012");
       // dvd.setManufacturer(" 北京海淀 ");
       // dvd.setName("Advd");
       // dvd.setRegioncode("2");
 
       // tr = session.beginTransaction();
       // session.save(dvd);
       // tr.commit();
 
       TDvd dvd = null ;
       TItem item = null ;
       String hql = "from TItem t where t.id=?" ;
       Query q = session.createQuery(hql);
       q.setParameter(0, "012" );
       Iterator it = q.list().iterator();
       while (it.hasNext()) {
           dvd = (TDvd) it.next();
       }
 
       System. out .println(dvd.getManufacturer());
    }
 
}
 
 
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值