日期:2014-05-16 浏览次数:20454 次
以《商品案例数据库为例》
首先
商品案例数据库设计:
管理员表:
??????? 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 Stri