类的继承(subclass)

 类的继承(subclass) - 398198920 - 冰冻三尺非一日之寒

item.sql

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[item]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[item]
GO

CREATE TABLE [dbo].[item] (
 [id] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [category] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [manufacturer] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [pagecount] [int] NULL ,
 [regioncode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

Item.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 - Hibernate Tools
-->
<hibernate-mapping>
    <class name="org.lxh.hibernate05.Item" table="item">
        <id name="id" type="string">
            <column name="id" length="32" />
            <generator class="assigned" />
        </id>
        <discriminator column="category" type="java.lang.String"></discriminator>
        <property name="name" type="string">
            <column name="name" length="20" not-null="true" />
        </property>
        <property name="manufacturer" type="string">
            <column name="manufacturer" length="20" not-null="true" />
        </property>
        <subclass name="org.lxh.hibernate05.TBook" discriminator-value="1">
                <property name="pagecount" type="integer"><column name="pagecount" /></property>
        </subclass>
        <subclass name="org.lxh.hibernate05.TDvd" discriminator-value="2">
                 <property name="regioncode" type="string"><column name="regioncode" length="2" /></property>
        </subclass>
    </class>
</hibernate-mapping>
hibernate2.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="dialect">
  org.hibernate.dialect.SQLServerDialect
 </property>
 <property name="connection.url">
  jdbc:sqlserver://localhost:1433;databaseName=mldn
 </property>
 <property name="connection.username">sa</property>
 <property name="connection.password"></property>
 <property name="connection.driver_class">
  com.microsoft.jdbc.sqlserver.SQLServerDriver
 </property>
 <property name="myeclipse.connection.profile">ss</property>
 <property name="show_sql">true</property>
 <mapping resource="org/lxh/hibernate05/Item.hbm.xml" />

</session-factory>

</hibernate-configuration>

Item.java

package org.lxh.hibernate05;

 

/**
 * Item generated by MyEclipse - Hibernate Tools
 */

public class Item  implements java.io.Serializable {


    // Fields   

     private String id;
     private String category;
     private String name;
     private String manufacturer;
     private String regionCode;
     private Integer pageCount;


    // Constructors

    /** default constructor */
    public Item() {
    }

 /** minimal constructor */
    public Item(String id, String category, String name, String manufacturer) {
        this.id = id;
        this.category = category;
        this.name = name;
        this.manufacturer = manufacturer;
    }
   
    /** full constructor */
    public Item(String id, String category, String name, String manufacturer, String regionCode, Integer pageCount) {
        this.id = id;
        this.category = category;
        this.name = name;
        this.manufacturer = manufacturer;
        this.regionCode = regionCode;
        this.pageCount = pageCount;
    }

  
    // Property accessors

    public String getId() {
        return this.id;
    }
   
    public void setId(String id) {
        this.id = id;
    }

    public String getCategory() {
        return this.category;
    }
   
    public void setCategory(String category) {
        this.category = category;
    }

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

    public String getManufacturer() {
        return this.manufacturer;
    }
   
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getRegionCode() {
        return this.regionCode;
    }
   
    public void setRegionCode(String regionCode) {
        this.regionCode = regionCode;
    }

    public Integer getPageCount() {
        return this.pageCount;
    }
   
    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }
}

TBook.java

package org.lxh.hibernate05;

public class TBook extends Item {
 private int pagecount;

 public int getPagecount() {
  return pagecount;
 }

 public void setPagecount(int pagecount) {
  this.pagecount = pagecount;
 }
}

TDvd.java

package org.lxh.hibernate05;

public class TDvd extends Item {
 private String regioncode;

 public String getRegioncode() {
  return regioncode;
 }

 public void setRegioncode(String regioncode) {
  this.regioncode = regioncode;
 }
}

ItemOperate.java

package org.lxh.hibernate05;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ItemOperate {
 private Session session=null;
 public ItemOperate(){
  Configuration config=new Configuration().configure("hibernate2.cfg.xml");
  SessionFactory factory=config.buildSessionFactory();
  this.session=factory.openSession();
 }
 public void insert(Item item){
  this.session.save(item);
  this.session.beginTransaction().commit();
 }
 public Item queryById(String id){
  Item item=null;
  String hql="from TItem as t where t.id=?";
  Query q=this.session.createQuery(hql);
  q.setString(0, id);
  Iterator iter=q.list().iterator();
  while(iter.hasNext()){
   item=(Item)iter.next();
  }
  return item;
 }
}
Test.java

package org.lxh.hibernate05;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ItemOperate to=new ItemOperate();
  TBook book=new TBook();
  book.setId("m003");
  book.setName("JAVA 2 EE");
  book.setManufacturer("www.mldn.cn");
  book.setPagecount(4444);
  to.insert(book);
  
//  TDvd dvd=new TDvd();
//  dvd.setId("m005");
//  dvd.setManufacturer("http://www.mldn.cn");
//  dvd.setName("J2EE");
//  dvd.setRegioncode("10");
//  to.insert(dvd);
  
//  TBook book=(TBook)to.queryById("m001");
//  System.out.println(book.getId());
//  System.out.println(book.getManufacturer());
//  System.out.println(book.getName());
//  System.out.println(book.getPagecount());
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值