实验七 Hibernate关联关系映射——登录用户的地址管理
一、基础实验——多对一/一对多关联
(一)实验目的
- 掌握 Hibernate 关联关系映射的基本概念,理解关联的方向和数量,重点理 解双向一对多/多对一的关联关系,及其在实际应用中的体现;
- 学习 Hibernate 框架处理一对多/多对一关联关系的方法,掌握关联关系中持 久化类的实现方法、以及相应 Hibernate 映射文件的配置方案;
- 能在实际应用中通过 Hibernate 建立正确的一对多/多对一关联关系映射,并 以面向对象的方式进行数据库访问。
(二)基本知识与原理
- 客观世界中的对象往往不孤立存在,例如老师与被授课的学生存在关联关 系,如果已经得到某老师的实例,那么应该可以获取该老师对应的全部学生, 反之如果已经得到一个学生的实例,也应该可以访问该学生对应的老师—— 这种实例之间的相互访问就是关联关系;Hibernate 框架可以处理各种不同 的关联关系;
- 关联的方向可分为单向关联和双向关联:
- 单向关联:只需单向访问关联端,例如只能通过老师访问学生,或者只能通过学生访问老师;
- 双向关联:关联的两端可以相互访问,例如老师和学生之间可以相互访问;
- 除考虑关联的方向问题之外,还要考虑关联双方的数量问题,即一对一、一对多、多对一、多对多的关联关系;
- 双向的一对多/多对一关系是现实中最为常见的关联关系,假设实体类A 到实体类B 是一对多(一个A 的实例关联多个B 的实例),则B 到A 就是多对一(多个B 的实例可能关联同一个A 的实例);要表示这种关系,则B
类(多的一端)中关联一个A 的实例,而A 类(一的一端)中关联一个集合对象,集合元素为B 的实例; - 在Hibernate 映射文件中,作为一的一端,需要使用<set…/>或<bag…/>元素来映射关联属性;作为多的一端,则需要使用<many-to-one…/>元素来映射关联属性。
(三)实验过程及其记录
-
在MySQL中创建一个名称为hibernatedb的数据库,并在该数据库中创建一个名称为customer的数据库,创建表的结构如下:
图1-1 customer数据表结构 -
在表customer中添加3条记录,具体如表7-1所示:
表1-2 customer中的记录 customerID account password 1 zjut zjut 2 admin admin 3 temp temp -
在hibernatedb数据库中创建一个名为address的数据表表,用于记录用户的联系地址,customer与address是一对多关系,其中address表中的cust_id是外键,参考customer表的主键:
-
新建Web工程hibernate-prj3,并添加MySQL驱动程序库文件、commons-loggin-1.2.jar、Struts2核心包和Hibernate核心包到工程中;
图1-4 jar包目录 -
在hibernate-prj3/src中新建hibernate配置文件hibernate.cfg.xml;
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://rm-bp10ju74719fp6g4emo.mysql.rds.aliyuncs.com:3306/hibernatedb?serverTimezone=GMT%2B8&useSSL=false</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">yiyi1333</property> <property name="connection.password">zzy@15712651279</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="connection.autocommit">true</property> <property name="hibernate.c3p0.max_size">10</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.timeout">2000</property> </session-factory> </hibernate-configuration>
-
新建cn.edu.zjut.po包,并在其中创建持久化类Customer.java和Address.java,Customer与Address是一对多的关系,因此需要在Customer中新增加一个Set属性,用于记录它关联的一系列Address实体,而在Daaress中只需增加一个Customer类型的属性:
package cn.edu.zjut.po; import java.util.Date; import java.util.HashSet; import java.util.Set; public class Customer { private int customerid; private String account; private String password; private String repassword; private String name; private Boolean sex; private String sexStr; private Date birthday; private String email; private Set addresses = new HashSet(0); public int getCustomerid() { return customerid; } public void setCustomerid(int customerid) { this.customerid = customerid; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRepassword() { return repassword; } public void setRepassword(String repassword) { this.repassword = repassword; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public String getSexStr() { return sexStr; } public void setSexStr(String sexStr) { this.sexStr = sexStr; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Set getAddresses() { return addresses; } public void setAddresses(Set addresses) { this.addresses = addresses; } }
package cn.edu.zjut.po; public class Address { private int addressid; private String detail; private String zipcode; private String phone; private String type; private Customer customer; public int getAddressid() { return addressid; } public void setAddressid(int addressid) { this.addressid = addressid; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
-
在cn.edu.zjut.po包中新建映射文件Customer.hbm.xml,作为一的一端,需要使用<set…/>或<bag…/>元素来映射关联属性,在<set…/>或<bag…/>元素中需要增加<key…/>子元素映射外键列,并使用<one-to-many…/>子元素映射关联属性:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.edu.zjut.po.Customer" table="customer" catalog="hibernatedb"> <id name="customerid" type="int"> <column name="customerid"/> <generator class="increment"/> </id> <property name="account" type="java.lang.String"> <column name="account" length="20" unique="true"/> </property> <property name="password" type="java.lang.String"> <column name="password" length="20"/> </property> <property name="name" type="java.lang.String"> <column name="name" length="20"/> </property> <property name="sexStr" type="java.lang.String"> <column name="sex"/> </property> <property name="birthday" type="java.util.Date"> <column name="birthday" length="10"/> </property> <property name="email" type="java.lang.String"> <column name="email" length="100"/> </property> <set name="customerid" inverse="true" cascade="all" lazy="false"> <key column="cust_id"/> <one-to-many class="cn.edu.zjut.po.Address"/> </set> </class> </hibernate-mapping>
-
在cn.edu.zjut.po包中新建映射文件Address.hbm.xml,作为多的一段,需要使用<many-to-one…/>元素来映射关联属性:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.edu.zjut.po.Address" table="address" catalog="hibernatedb"> <id name="addressid" type="int"> <column name="addressid"/> <generator class="increment"/> </id> <property name="detail" type="java.lang.String"> <column name="detail" length="200"/> </property> <property name="zipcode" type="java.lang.String"> <column name="zipcode" length="10"/> </property> <property name="phone" type="java.lang.String"> <column name="phone" length="20"/> </property> <property name="type" type="java.lang.String"> <column name="type" length="20"/> </property> <many-to-one name="customer" class="cn.edu.zjut.po.Customer" fetch="select" not-null="true"> <column name="cust_id"/> </many-to-one> </class> </hibernate-mapping>
-
修改配置文件hibernate.cfg.xml,增加Customer.hbm.xml与Address.hbm.xml映射文件的声明;
-
新建cn.edu,zjut.dao包,并在其中创建DAO操作辅助类HibernateUtil.java和数据库操作基础类BaseHibernateDAO.java;
package cn.edu.zjut.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATIO