以《商品案例数据库为例》
首先
商品案例数据库设计:
管理员表:
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 class="native"/>
</id>
<property name="name" 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 class="native"/>
</id>
<property name="name" 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 class="cn.csdn.domain.Orders" />
</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 class="native"/>
</id>
<property name="number" type="string" length="30"/>
<many-to-one name="customer"
class="cn.csdn.domain.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 class="cn.csdn.domain.OrdersItem"/>
//订单表和订单明细表是一对多的关系
</set>
</class>
商品表:
一张订单可以有多个商品,一个商品可以有多个订单
所以:订单表和商品表是多对多的关系
订单明细表和商品表是多对一的关系
public class Goods implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String type;
private double price;
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;
}
省略 Get,set方法
}
映射文件中的代码:
<class name="cn.csdn.domain.Goods" table="goods" catalog="db">
<id name="id">
<generator class="native"/>
</id>
<property name="name" 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 class="cn.csdn.domain.OrdersItem"/>
</set>
</class>
订单明细表:
public class OrdersItem implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private Goods goods;
private int num;
private double total;
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;
}
省略 Get,set方法
}
映射文件中的代码:
<class name="cn.csdn.domain.OrdersItem" table="ordersItems"
catalog="db">
<id name="id">
<generator class="native" />
</id>
<many-to-one name="goods" class="cn.csdn.domain.Goods"
column="gid" />
<property name="num" type="integer" />
<property name="total" type="double" />
<many-to-one name="orders" class="cn.csdn.domain.Orders"
column="oid" />
</class>
总结:一方主键做多方外键,一方用set集合多方用一方对象