假设有一个商品管理项目。
商品只有以下两类:1、书。 2、DVD
有以下共通的属性:编号,标题,价格
书包含自己单独的属性:页数
DVD包含自己单独的属性:时间长度
类的设计为:
三个类:商品类、子类书类、子类DVD类
一、两张表
2张表:书表和DVD表,通过单独操作书与DVD来完成管理操作。
该设计与父类和子类无关,只是通过操作单独的子类来完成功能。
二、三张表
商品表:编号,标题,价格
书表:商品编号,页数
DVD表:商品编号,时间长度
1、建立数据库
DROP TABLE item ;
DROP TABLE book ;
DROP TABLE dvd ;
CREATE TABLE item (
id int primary key auto_increment ,
title varchar(20) not null,
price double not null
);
CREATE TABLE book (
id int primary key ,
page int not null
);
CREATE TABLE dvd (
id int primary key ,
time_length double not null
);
2、生成映射
只需要生成item表的映射,另外两个类手工编写,并且编写过程中不需要加入book和dvd的映射关系文件。
package org.liky.pojo;
public class Item implements java.io.Serializable {
// Fields
private Integer id;
private String title;
private Double price;
// Constructors
public Item() {
}
public Item(String title, Double price) {
this.title = title;
this.price = price;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
}
建立Book类
package org.liky.pojo;
public class Book extends Item {
private int page;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
}
建立DVD类
package org.liky.pojo;
public class Dvd extends Item {
private double timeLength;
public double getTimeLength() {
return timeLength;
}
public void setTimeLength(double timeLength) {
this.timeLength = timeLength;
}
3、修改映射文件
修改映射文件,加入子类配置
<?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="org.liky.pojo.Item" table="item" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="20" not-null="true" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0"
not-null="true" />
</property>
<!-- 表示Item类包含一个子类为Book,Book类与book表有对应关系 -->
<joined-subclass name="org.liky.pojo.Book" table="book">
<!-- 表示在book表中是通过id字段与item表建立关联关系的 -->
<key column="id"></key>
<!-- 表示Book类中的page属性与book表中的page字段有对应关系 -->
<property name="page" type="java.lang.Integer">
<column name="page" not-null="true"></column>
</property>
</joined-subclass>
<joined-subclass name="org.liky.pojo.Dvd" table="dvd">
<key column="id"></key>
<property name="timeLength" type="java.lang.Double">
<column name="time_length" not-null="true"></column>
</property>
</joined-subclass>
</class>
</hibernate-mapping>
4、编写后台代码
只建立Item类的DAO接口等操作
5、测试
// 插入书
// Book book = new Book();
// book.setTitle("哈里波特");
// book.setPrice(32.5);
// book.setPage(500);
//
// try {
// itemdao.doCreate(book);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 插入DVD
Dvd dvd = new Dvd();
dvd.setTitle("终结者");
dvd.setPrice(5.0);
dvd.setTimeLength(2.0);
try {
itemdao.doCreate(dvd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
List all = itemdao.findAll(1, 10);
Iterator iter = all.iterator();
while (iter.hasNext()) {
Item item = (Item) iter.next();
if (item instanceof Book) {
Book book = (Book)item;
System.out.println(book.getTitle() + " --> " +book.getPage());
} else if (item instanceof Dvd) {
Dvd dvd = (Dvd) item;
System.out.println(dvd.getTitle() + " --> " +dvd.getTimeLength());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
查询性能非常低,因此绝对不使用。
三、一张表
1张表:只有一个Item表
Item表:编号、标题、价格、页数、时间长度、标志位
1、建立表
DROP TABLE item ;
DROP TABLE book ;
DROP TABLE dvd ;
CREATE TABLE item (
id int primary key auto_increment ,
title varchar(20) not null,
price double not null,
page int ,
time_length double ,
flag varchar(20) not null
);
标志位使用varchar类型,会由Hibernate自动进行处理
2、生成映射类
3、修改映射文件
<?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="org.liky.pojo.Item" table="item" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<!-- 定义标志位的值,标志位对应的字段为flag,其类型按照String类型处理 -->
<discriminator column="flag"></discriminator>
<property name="title" type="java.lang.String">
<column name="title" length="20" not-null="true" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0"
not-null="true" />
</property>
<!-- 表示Item类包含一个子类Book,如果操作Book时,标志位的值为book -->
<subclass name="org.liky.pojo.Book" discriminator-value="book">
<property name="page" type="java.lang.Integer">
<column name="page"></column>
</property>
</subclass>
<!-- 表示Item类包含一个子类Dvd,如果操作Dvd时,标志位的值为dvd -->
<subclass name="org.liky.pojo.Dvd" discriminator-value="dvd">
<property name="timeLength" type="java.lang.Double">
<column name="time_length"></column>
</property>
</subclass>
</class>
</hibernate-mapping>
4、测试
其他的任何后台代码都不需要修改,Hibernate会自行处理。
// 插入书
// Book book = new Book();
// book.setTitle("哈里波特");
// book.setPrice(32.5);
// book.setPage(500);
//
// try {
// itemdao.doCreate(book);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 插入DVD
// Dvd dvd = new Dvd();
// dvd.setTitle("终结者");
// dvd.setPrice(5.0);
// dvd.setTimeLength(2.0);
//
// try {
// itemdao.doCreate(dvd);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
List all = itemdao.findAll(1, 10);
Iterator iter = all.iterator();
while (iter.hasNext()) {
Item item = (Item) iter.next();
if (item instanceof Book) {
Book book = (Book) item;
System.out.println(book.getTitle() + " --> "
+ book.getPage());
} else if (item instanceof Dvd) {
Dvd dvd = (Dvd) item;
System.out.println(dvd.getTitle() + " --> "
+ dvd.getTimeLength());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
要使用的话必然使用单表的这种方式。
商品只有以下两类:1、书。 2、DVD
有以下共通的属性:编号,标题,价格
书包含自己单独的属性:页数
DVD包含自己单独的属性:时间长度
类的设计为:
三个类:商品类、子类书类、子类DVD类
一、两张表
2张表:书表和DVD表,通过单独操作书与DVD来完成管理操作。
该设计与父类和子类无关,只是通过操作单独的子类来完成功能。
二、三张表
商品表:编号,标题,价格
书表:商品编号,页数
DVD表:商品编号,时间长度
1、建立数据库
DROP TABLE item ;
DROP TABLE book ;
DROP TABLE dvd ;
CREATE TABLE item (
id int primary key auto_increment ,
title varchar(20) not null,
price double not null
);
CREATE TABLE book (
id int primary key ,
page int not null
);
CREATE TABLE dvd (
id int primary key ,
time_length double not null
);
2、生成映射
只需要生成item表的映射,另外两个类手工编写,并且编写过程中不需要加入book和dvd的映射关系文件。
package org.liky.pojo;
public class Item implements java.io.Serializable {
// Fields
private Integer id;
private String title;
private Double price;
// Constructors
public Item() {
}
public Item(String title, Double price) {
this.title = title;
this.price = price;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
}
建立Book类
package org.liky.pojo;
public class Book extends Item {
private int page;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
}
建立DVD类
package org.liky.pojo;
public class Dvd extends Item {
private double timeLength;
public double getTimeLength() {
return timeLength;
}
public void setTimeLength(double timeLength) {
this.timeLength = timeLength;
}
3、修改映射文件
修改映射文件,加入子类配置
<?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="org.liky.pojo.Item" table="item" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="20" not-null="true" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0"
not-null="true" />
</property>
<!-- 表示Item类包含一个子类为Book,Book类与book表有对应关系 -->
<joined-subclass name="org.liky.pojo.Book" table="book">
<!-- 表示在book表中是通过id字段与item表建立关联关系的 -->
<key column="id"></key>
<!-- 表示Book类中的page属性与book表中的page字段有对应关系 -->
<property name="page" type="java.lang.Integer">
<column name="page" not-null="true"></column>
</property>
</joined-subclass>
<joined-subclass name="org.liky.pojo.Dvd" table="dvd">
<key column="id"></key>
<property name="timeLength" type="java.lang.Double">
<column name="time_length" not-null="true"></column>
</property>
</joined-subclass>
</class>
</hibernate-mapping>
4、编写后台代码
只建立Item类的DAO接口等操作
5、测试
// 插入书
// Book book = new Book();
// book.setTitle("哈里波特");
// book.setPrice(32.5);
// book.setPage(500);
//
// try {
// itemdao.doCreate(book);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 插入DVD
Dvd dvd = new Dvd();
dvd.setTitle("终结者");
dvd.setPrice(5.0);
dvd.setTimeLength(2.0);
try {
itemdao.doCreate(dvd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
List all = itemdao.findAll(1, 10);
Iterator iter = all.iterator();
while (iter.hasNext()) {
Item item = (Item) iter.next();
if (item instanceof Book) {
Book book = (Book)item;
System.out.println(book.getTitle() + " --> " +book.getPage());
} else if (item instanceof Dvd) {
Dvd dvd = (Dvd) item;
System.out.println(dvd.getTitle() + " --> " +dvd.getTimeLength());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
查询性能非常低,因此绝对不使用。
三、一张表
1张表:只有一个Item表
Item表:编号、标题、价格、页数、时间长度、标志位
1、建立表
DROP TABLE item ;
DROP TABLE book ;
DROP TABLE dvd ;
CREATE TABLE item (
id int primary key auto_increment ,
title varchar(20) not null,
price double not null,
page int ,
time_length double ,
flag varchar(20) not null
);
标志位使用varchar类型,会由Hibernate自动进行处理
2、生成映射类
3、修改映射文件
<?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="org.liky.pojo.Item" table="item" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<!-- 定义标志位的值,标志位对应的字段为flag,其类型按照String类型处理 -->
<discriminator column="flag"></discriminator>
<property name="title" type="java.lang.String">
<column name="title" length="20" not-null="true" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0"
not-null="true" />
</property>
<!-- 表示Item类包含一个子类Book,如果操作Book时,标志位的值为book -->
<subclass name="org.liky.pojo.Book" discriminator-value="book">
<property name="page" type="java.lang.Integer">
<column name="page"></column>
</property>
</subclass>
<!-- 表示Item类包含一个子类Dvd,如果操作Dvd时,标志位的值为dvd -->
<subclass name="org.liky.pojo.Dvd" discriminator-value="dvd">
<property name="timeLength" type="java.lang.Double">
<column name="time_length"></column>
</property>
</subclass>
</class>
</hibernate-mapping>
4、测试
其他的任何后台代码都不需要修改,Hibernate会自行处理。
// 插入书
// Book book = new Book();
// book.setTitle("哈里波特");
// book.setPrice(32.5);
// book.setPage(500);
//
// try {
// itemdao.doCreate(book);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 插入DVD
// Dvd dvd = new Dvd();
// dvd.setTitle("终结者");
// dvd.setPrice(5.0);
// dvd.setTimeLength(2.0);
//
// try {
// itemdao.doCreate(dvd);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
List all = itemdao.findAll(1, 10);
Iterator iter = all.iterator();
while (iter.hasNext()) {
Item item = (Item) iter.next();
if (item instanceof Book) {
Book book = (Book) item;
System.out.println(book.getTitle() + " --> "
+ book.getPage());
} else if (item instanceof Dvd) {
Dvd dvd = (Dvd) item;
System.out.println(dvd.getTitle() + " --> "
+ dvd.getTimeLength());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
要使用的话必然使用单表的这种方式。