數據庫的設計及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的映射實現數據表的生成

首先先生成管理員表

代碼如下:

publicclass Admin implements Serializable{

privatestaticfinallongserialVersionUID = 1L;

privateintid;

private String name;

private String pass;

public Admin() {}

public Admin(String name, String pass) {

this.name = name;

this.pass = pass;

}

省略Getset方法

}

映射文件中的代碼:

<hibernate-mapping>

<classname="cn.csdn.domain.Admin"table="admins"catalog="db">

<idname="id">

<generatorclass="native"/>

</id>

<propertyname="name"type="string"length="30"/>

<propertyname="pass"type="string"length="12"/>

</class>

</hibernate-mapping>

接下來生成顧客表和訂單表

顧客表:顧客表和訂單表是一對多的關係

publicclass Customer implements Serializable {

privatestaticfinallongserialVersionUID = 1L;

privateintid;

private String name;

private String pass;

private Sex sex; //enum

private String email;

private Date rdate; //BeanUtils

privateintstate;

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() {}

省略Getset方法

}

映射文件中的代碼:

<classname="cn.csdn.domain.Customer"table="customers"catalog="db">

<idname="id">

<generatorclass="native"/>

</id>

<propertyname="name"type="string"length="30"/>

<propertyname="pass"type="string"length="12"/>

<propertyname="sex"type="string"length="4"/>

<propertyname="email"type="string"length="30"/>

<propertyname="rdate"type="timestamp"/>

<propertyname="state"type="integer"/>

<setname="orders"table="orders">

<keycolumn="cid"/>//cid是訂單表的外鍵,顧客表的主鍵

<one-to-manyclass="cn.csdn.domain.Orders"/>

</set>

</class>

訂單表:

顧客表和訂單表是一對多的關係;

訂單表和商品表是多對多的關係,所以要加一個表,變成兩一對多的實體映射

訂單表和訂單明細表是一對多的關係,商品表和訂單明細表是一對多的關係

所以就加了一個訂單明細表

publicclass Orders implements Serializable{

privatestaticfinallongserialVersionUID = 1L;

privateintid;

private String number;

private Customer customer;

private Date odate;

privateintstate;

privateintQstate;

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;

}

省略Getset方法

}

映射文件中的代碼:

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

<idname="id">

<generatorclass="native"/>

</id>

<propertyname="number"type="string"length="30"/>

<many-to-onename="customer"

class="cn.csdn.domain.Customer"column="cid"/>

//訂單表和顧客表是多對一的關係

<propertyname="odate"type="timestamp"/>

<propertyname="state"type="integer"/>

<propertyname="Qstate"type="integer"/>

<setname="ordersItem">

<keycolumn="oid"/>//訂單明細表中的oid外鍵是訂單表的主鍵

<one-to-manyclass="cn.csdn.domain.OrdersItem"/>

//訂單表和訂單明細表是一對多的關係

</set>

</class>

商品表:

一張訂單可以有多個商品,一個商品可以有多個訂單

所以:訂單表和商品表是多對多的關係

訂單明細表和商品表是多對一的關係

publicclass Goods implements Serializable {

privatestaticfinallongserialVersionUID = 1L;

privateintid;

private String name;

private String type;

privatedoubleprice;

private Set<OrdersItem> ordersItem = new HashSet<OrdersItem>();


public Goods() {}

public Goods(String name, String type, double price,

Set<OrdersItem> ordersItem) {

this.name = name;

this.type = type;

this.price = price;

this.ordersItem = ordersItem;

}

省略Getset方法


}

映射文件中的代碼:

<classname="cn.csdn.domain.Goods"table="goods"catalog="db">

<idname="id">

<generatorclass="native"/>

</id>

<propertyname="name"type="string"length="50"/>

<propertyname="type"type="string"length="40"/>

<propertyname="price"type="double"/>

<setname="ordersItem">

<keycolumn="gid"/>

<one-to-manyclass="cn.csdn.domain.OrdersItem"/>

</set>

</class>

訂單明細表:

publicclass OrdersItem implements Serializable{

privatestaticfinallongserialVersionUID = 1L;

privateintid;

private Goods goods;

privateintnum;

privatedoubletotal;

private Orders orders;

public OrdersItem() {}

public OrdersItem(Goods goods, int num, double total, Orders orders){

this.goods = goods;

this.num = num;

this.total = total;

this.orders = orders;

}

省略Getset方法

}

映射文件中的代碼:

<classname="cn.csdn.domain.OrdersItem"table="ordersItems"

catalog="db">

<idname="id">

<generatorclass="native"/>

</id>

<many-to-onename="goods"class="cn.csdn.domain.Goods"

column="gid"/>

<propertyname="num"type="integer"/>

<propertyname="total"type="double"/>

<many-to-onename="orders"class="cn.csdn.domain.Orders"

column="oid"/>

</class>

總結:一方主鍵做多方外鍵,一方用set集合多方用一方對象


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章