首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

数据库的设计及hibernate实业映射

2012-09-18 
数据库的设计及hibernate实体映射以《商品案例数据库为例》首先商品案例数据库设计:管理员表:??????? id????

数据库的设计及hibernate实体映射

以《商品案例数据库为例》

首先

商品案例数据库设计:

管理员表:

??????? id???? :递增序列

??????? name? :管理员

??????? pass?? :密码

客户表

??????? id???? :递增序列

??????? name? :登录名称

??????? pass?? :登录密码

??????? sex??? : 性别??????? //enum

??????? email? :注册邮箱???

??????? rdate?? :注册日期??? //BeanUtils

??????? state?? :是否被激活的状态? 0 1

商品表

??????? id???? :递增序列

??????? name? : 商品的名称

??????? type?? : 商品的类型

??????? price?? :商品的价格

订单表:

??????? Id???????? :递增序列

??????? number??? :订单编号

? customerid? :客户名称

??????? odate????? :订单时间

??????? state?????? :订单的状态 //已发货? 未发货? 0 1

??????? ostate????? :确认订单是否订购???????????? 0 1

??? 订单明细表

??????? id

??????? goodid?? :商品的名称

??????? num???? :商品数量

??????? total???? :商品的总价格

??????? ordersid? :订单

一张订单可以有多个商品,一个商品可以有多个订单

所以:订单表和商品表是多对多的关系

一个顾客可以有多个订单,一个订单只能属于一个顾客

所以:顾客表和订单表是一对多的关系

?

要实现Hibernate实体映射首先要导入hibernate所需要的包

?

接下来用hibernate的映射实现数据表的生成

首先先生成管理员表

代码如下:

public class Admin implements Serializable{
?????? private static final long serialVersionUID = 1L;
?????? private int id;
?????? private String name;
?????? private String pass;
?????? public Admin() {}
?????? public Admin(String name, String pass) {
?????????? this.name = name;
?????????? this.pass = pass;
?????? }
????? 省略 Get,set方法
}
映射文件中的代码:

<hibernate-mapping>
?????? <class name="cn.csdn.domain.Admin" table="admins" catalog="db">

???????????? <id name="id">

???????????? <generator type="string" length="30"/>

???????????? <property name="pass" type="string" length="12"/>

?????? </class>
</hibernate-mapping>
接下来生成顾客表和订单表

?????? 顾客表:顾客表和订单表是一对多的关系

public class Customer implements Serializable {
??? private static final long serialVersionUID = 1L;
??? private int id;
??? private String name;
??? private String pass;
??? private Sex sex; //enum

??? private String email;
??? private Date rdate; //BeanUtils

??? private int state;
??? private Set<Orders> orders = new HashSet<Orders>();
??? public Customer(String name, String pass, Sex sex, String email,
?????????? Date rdate, int state, Set<Orders> orders) {
?????? this.name = name;
?????? this.pass = pass;
?????? this.sex = sex;
?????? this.email = email;
?????? this.rdate = rdate;
?????? this.state = state;
?????? this.orders = orders;
??? }
??? public Customer() {}
省略 Get,set方法
}
映射文件中的代码:

? <class name="cn.csdn.domain.Customer" table="customers" catalog="db">

?????? <id name="id">

????????? <generator type="string" length="30" />

?????? <property name="pass" type="string" length="12" />

?????? <property name="sex" type="string" length="4" />

?????? <property name="email" type="string" length="30" />

?????? <property name="rdate" type="timestamp" />

?????? <property name="state" type="integer" />

???? <set name="orders" table="orders">

?????????? <key column="cid" />//cid是订单表的外键,顾客表的主键
?????????? <one-to-many />

???? </set>
??? </class>
订单表:

顾客表和订单表是一对多的关系;

??????? 订单表和商品表是多对多的关系,所以要加一个表,变成两一对多的实体映射

??????? 订单表和订单明细表是一对多的关系,商品表和订单明细表是一对多的关系

所以就加了一个订单明细表

public class Orders implements Serializable{
??? private static final long serialVersionUID = 1L;
??? private int id;
??? private String number;
??? private Customer customer;
??? private Date odate;
??? private int state;
??? private int Qstate;
??? private Set<OrdersItem> ordersItem = new HashSet<OrdersItem>();

?

??? public Orders() { }
??? public Orders(int id, String number, Customer customer, Date odate,
?????????? int state, int qstate, Set<OrdersItem> ordersItem) {
?????? this.id = id;
?????? this.number = number;
?????? this.customer = customer;
?????? this.odate = odate;
?????? this.state = state;
?????? Qstate = qstate;
?????? this.ordersItem = ordersItem;
??? }

省略 Get,set方法
}
映射文件中的代码:

<class name="cn.csdn.domain.Orders" table="orders" catalog="db">

?????? <id name="id">

????????? <generator type="string" length="30"/>

?<many-to-one name="customer"

column="cid"/>

//订单表和顾客表是多对一的关系
?????? <property name="odate" type="timestamp" />

?????? <property name="state" type="integer" />

?????? <property name="Qstate" type="integer" />

?????? <set name="ordersItem">

???????? <key column="oid"/>//订单明细表中的oid外键是订单表的主键
???????? <one-to-many table="goods" catalog="db">

?????? <id name="id">

????????? <generator type="string" length="50"/>

?????? <property name="type" type="string" length="40"/>

?????? <property name="price" type="double"/>

?????? <set name="ordersItem">

???????? <key column="gid"/>

???????? <one-to-many table="ordersItems"

?????? catalog="db">

?????? <id name="id">

?????????? <generator />

?????? </id>
<many-to-one name="goods" />

?????? <property name="num" type="integer" />

?????? <property name="total" type="double" />

?????? <many-to-one name="orders" />

</class>

总结:一方主键做多方外键,一方用set集合多方用一方对象

?

热点排行