http://www.blogjava.net/xiaodu/archive/2008/07/11/214188.html
hiberate继承分为三种方式:一个表对应所有继承的类,具体类对应一个表,一个类对应一个表(父类及子类)
一个类对应一个表(父类及子类)
数据库脚本
CREATE TABLE `t_item1`
(
`id` int(11)
NOT NULL
AUTO_INCREMENT,
`manufacture`
varchar(20) DEFAULT
NULL,
`name`
varchar(20) DEFAULT
NULL,
PRIMARY KEY
(`id`)
) ENGINE=InnoDB DEFAULT
CHARSET=latin1 ROW_FORMAT=DYNAMIC
CREATE TABLE `t_book1`
(
`id` int(11)
NOT NULL
AUTO_INCREMENT,
`name`
varchar(20) DEFAULT
NULL,
`manufacture`
varchar(20) DEFAULT
NULL,
`pagecount`
int(11) DEFAULT
NULL,
PRIMARY KEY
(`id`)
) ENGINE=InnoDB DEFAULT
CHARSET=latin1 ROW_FORMAT=DYNAMIC COMMENT=‘InnoDB free: 3072 kB‘
CREATE TABLE `t_dvd1`
(
`id` int(11) NOT NULL
AUTO_INCREMENT,
`name` varchar(20)
DEFAULT
NULL,
`manufacture`
varchar(20) DEFAULT
NULL,
`regioncode`
varchar(20) DEFAULT
NULL,
PRIMARY KEY
(`id`)
) ENGINE=InnoDB DEFAULT
CHARSET=latin1 ROW_FORMAT=DYNAMIC
类代码
public class TItem1 implements Serializable {
private int id;
private String name;
private String manufacture;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacture() {
return manufacture;
}
public void setManufacture(String manufacture) {
this.manufacture = manufacture;
}
}
public class TBook1 extends TItem1 implements Serializable {
private int pagecount;
public int getPagecount() {
return pagecount;
}
public void setPagecount(int pagecount) {
this.pagecount =
pagecount;
}
}
public class TDvd1 extends TItem1 implements Serializable {
private String
regioncode;
public String getRegioncode() {
return regioncode;
}
public void setRegioncode(String regioncode) {
this.regioncode =
regioncode;
}
}
配置文件
< ?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>
< class name="mypackage.TItem1"
table="t_item1">
< id name="id" column="id">
<generator class="identity"/>
< /id>
< property
name="name" column="name"/>
< property name="manufacture"
column="manufacture"/>
< joined-subclass name="mypackage.TBook1"
table="t_book1">
<key column="id"/>
<property
name="pagecount" column="pagecount" type="java.lang.Integer"/>
< /joined-subclass>
< joined-subclass
name="mypackage.TDvd1" table="t_dvd1">
<key column="id"/>
<property name="regioncode" column="regioncode"
type="java.lang.String"/>
< /joined-subclass>
< /class>
< /hibernate-mapping>
具体类对应一个表
数据库脚本
CREATE TABLE `t_book2`
(
`id` int(11)
NOT NULL
AUTO_INCREMENT,
`name`
varchar(20) DEFAULT
NULL,
`manufacture`
varchar(20) DEFAULT
NULL,
`pagecount`
int(11) DEFAULT
NULL,
PRIMARY KEY
(`id`)
) ENGINE=MyISAM
AUTO_INCREMENT=17 DEFAULT CHARSET=latin1
CREATE TABLE `t_dvd2`
(
`id` int(11) NOT NULL
AUTO_INCREMENT,
`name` varchar(23)
DEFAULT
NULL,
`manufacture`
varchar(20) DEFAULT
NULL,
`regioncode`
varchar(20) DEFAULT
NULL,
PRIMARY KEY
(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=18
DEFAULT CHARSET=latin1
类代码
public abstract class TItem2 implements java.io.Serializable {
private int id;
private String name;
private String
manufacture;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacture() {
return manufacture;
}
public void setManufacture(String manufacture) {
this.manufacture = manufacture;
}
}
public class TDvd2 extends TItem2 implements Serializable {
private String regioncode;
public String getRegioncode() {
return regioncode;
}
public void setRegioncode(String regioncode) {
this.regioncode = regioncode;
}
}
public class TBook2 extends TItem2 implements Serializable {
private int pagecount;
public int getPagecount() {
return pagecount;
}
public void setPagecount(int pagecount) {
this.pagecount =
pagecount;
}
}
配置文件
< ?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>
< class name="mypackage.TDvd2"
table="t_dvd2">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property
name="name" column="name"/>
<property name="manufacture"
column="manufacture"/>
<property name="regioncode"
column="regioncode"/>
< /class>
< class
name="mypackage.TBook2" table="t_book2">
<id name="id"
column="id">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
<property name="manufacture" column="manufacture"/>
<property
name="pagecount" column="pagecount"/>
< /class>
< /hibernate-mapping>
一个表对应所有继承的类
数据库脚本
CREATE TABLE `t_item3`
(
`id` int(11)
NOT NULL
AUTO_INCREMENT,
`name`
varchar(20) DEFAULT
NULL,
`manufacture`
varchar(20) DEFAULT
NULL,
`pagecount`
int(11) DEFAULT
NULL,
`regioncode`
varchar(20) DEFAULT
NULL,
`category`
varchar(11) DEFAULT
NULL,
PRIMARY KEY
(`id`)
) ENGINE=MyISAM
AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
类代码
public class TItem3 implements Serializable {
private int id;
private String name;
private String manufacture;
private String category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacture() {
return manufacture;
}
public void setManufacture(String manufacture) {
this.manufacture = manufacture;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category =
category;
}
}
public class TDvd3 extends TItem3 implements Serializable {
private String regioncode;
public String getRegioncode() {
return regioncode;
}
public void setRegioncode(String regioncode) {
this.regioncode = regioncode;
}
}
public class TBook3 extends TItem3 implements Serializable {
private int pagecount;
public int getPagecount() {
return pagecount;
}
public void setPagecount(int pagecount) {
this.pagecount =
pagecount;
}
}
配置文件
< ?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>
< class name="mypackage.TItem3"
table="t_item3">
< id name="id" column="id">
<generator class="identity"/>
< /id>
< discriminator column="category" type="java.lang.String"/>
< property name="name" column="name"/>
< property
name="manufacture" column="manufacture"/>
< subclass
name="mypackage.TDvd3" discriminator-value="1">
<property
name="regioncode" column="regioncode"/>
< /subclass>
< subclass name="mypackage.TBook3" discriminator-value="0">
<property name="pagecount" column="pagecount"/>
< /subclass>
< /class>
< /hibernate-mapping>
三种方式的优缺点
每个类对应一个表
缺点:创建表过多,查询需用求外连接,优点:某个表字段发生变化只需修改相对表,支持多态查询,符合关系型数据库设计
具体类对应一个表
缺点:数据表包含重复字段,如果父类发生变化子类也要变化,查询父类时要查询所有子类,不支持多态查询,优点:符合关系型数据库设计
一个表对应所有类
缺点:表中引入区分子类的字段,子类的字段不能创建为空,优点:只需一张表,有很好的查询性能,只要修改一张表,支持多态查询