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

首先

商品案例数据库设计:

管理员表:

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 = new HashSet();

public Customer(String name, String pass, Sex sex, String email,

Date rdate, int state, Set 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 = new HashSet();


public Orders() {}

public Orders(int id, String number, Customer customer, Date odate,

int state, int qstate, Set 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 = new HashSet();


public Goods() {}

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

Set 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集合多方用一方对象