Hibernate实体层设计

本文介绍了一种商品管理系统的设计方案,包括两种不同的数据库表结构设计:一种是使用三张表分别存储商品的基本信息及其子类信息;另一种是使用一张表存储所有信息,并通过标志位区分不同类型的子类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设有一个商品管理项目。

商品只有以下两类: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();

}

要使用的话必然使用单表的这种方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值