一 一对多表配置
一对多就会一张表内容分数据对应另外一个表中多条数据,我们以客户(Custum)和客户联系人(LinkMan)为例。
1 编写实体类 (Custum 和 LinkMan)
代码如下:
public class Custum {
private Integer cid;
private String custName;
private String custLevel;
private String custSource;
private String custPhone;
private String custMobile;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustMobile() {
return custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
}
@Override
public String toString() {
return "Custum [getCid()=" + getCid() + ", getCustName()="
+ getCustName() + ", getCustLevel()=" + getCustLevel()
+ ", getCustSource()=" + getCustSource() + ", getCustPhone()="
+ getCustPhone() + ", getCustMobile()=" + getCustMobile() + "]";
}
public class LinkMan {
private Integer lkm_id;
private String lkm_name;
private String lkm_gender;
private String lkm_phone;
public Integer getLkm_id() {
return lkm_id;
}
public void setLkm_id(Integer lkm_id) {
this.lkm_id = lkm_id;
}
public String getLkm_name() {
return lkm_name;
}
public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
}
public String getLkm_gender() {
return lkm_gender;
}
public void setLkm_gender(String lkm_gender) {
this.lkm_gender = lkm_gender;
}
public String getLkm_phone() {
return lkm_phone;
}
public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
@Override
public String toString() {
return "LinkMan [getLkm_id()=" + getLkm_id() + ", getLkm_name()="
+ getLkm_name() + ", getLkm_gender()=" + getLkm_gender()
+ ", getLkm_phone()=" + getLkm_phone() + "]";
}
2 让两个实体类之间互相表示,在客户实体类中有多个联系人,在联系人表中有一个客户,hibernate 要求使用集合表示多个数据,要求使用set 集合。
因此在Cumtum 实体类中添加如下代码:
// 一个客户对应多个联系人,hibermate要求使用集合表示数据,使用set集合
private Set<LinkMan> linkmans = new HashSet<LinkMan>();
public Set<LinkMan> getLinkmans() {
return linkmans;
}
public void setLinkmans(Set<LinkMan> linkmans) {
this.linkmans = linkmans;
}
在联系人实体类中(LinkMan)中添加如下代码:
//一个联系人只能属于一个客户
private Custum custum ;
public Custum getCustum() {
return custum;
}
public void setCustum(Custum custum) {
this.custum = custum;
}
3 配置客户(Custum)和联系人(LinkMan)基本映射文件,Custum.hbm.xm,LinkMan.hbm.xml
(1)Custum.hbm.xml 配置文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
// class 标签name属性值是 Custum 实体类所在的路径,table是hibernate 自动生成实体类对应表的表名称
<class name="com.grl.entity.Custum" table="t_custum">
<id name="cid" column="cid">
<generator class="native"></generator>
</id>
<property name="custName" column="custName"></property>
<property name="custLevel" column="custLevel"></property>
<property name="custSource" column="custSource"></property>
<property name="custPhone" column="custPhone"></property>
<property name="custMobile" column="custMobile"></property>
<!-- set 标签就是用来配置表之间的对应关系的 name属性值 是Custum实体类中 对应的 多个联系人的set集合名称 ,inverse 值为TRUE时,表示custum 放弃对外键的维护-->
<set name="linkmans" inverse="true">
<!-- key 标签 column 属性值是 外键的字段值-->
<key column="clid"></key>
<!-- class 属性是 LinkMan实体类的路径 -->
<one-to-many class = "com.grl.entity.LinkMan"/>
</set>
</class>
</hibernate-mapping>
(2)LinkMan.hbm.xml 文件配置代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- class name属性值是 LinkMan 实体类所在的路径 ,table 是 实体类对应数据库表的表名称-->
<class name="com.grl.entity.LinkMan" table="t_linkman">
<id name="lkm_id" column="lkm_id">
<generator class="native"></generator>
</id>
<property name="lkm_name" column="lkm_name"></property>
<property name="lkm_gender" column="lkm_gender"></property>
<property name="lkm_phone" column="lkm_phone"></property>
<!-- class 对应的 Custum 实体所在的路径 column 是该表的外键名称 与Custum.hbm.xml 配置 <key column="clid"></key> 配置的属性值相同-->
<many-to-one name="custum" class = "com.grl.entity.Custum" column="clid"></many-to-one>
</class>
</hibernate-mapping>
4 创建核心配置文件(必须在src 下面,名称必须是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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateDB</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">170grl</property>
<property name="hibernate.show_sql ">true</property>
<property name="hibernate.format_sql ">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/grl/entity/Custum.hbm.xml" />
<mapping resource="com/grl/entity/LinkMan.hbm.xml" />
</session-factory>
</hibernate-configuration>
5 配置完毕 ,测试配置结果,执行单元测试,就会在数据库中建立两张表
@Test
public void test()
{
SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
}
二 一对做级联操作
1 一对多的级联保存操作
(1)代码如下:
@Test
public void add()
{
Session session = HibernateUtils.getSession();
Transaction ts = session.beginTransaction();
//声明一个客户实体类
Custum custum = new Custum();
custum.setCustName("传智博客");
custum.setCustLevel("VIP");
custum.setCustSource("网络");
custum.setCustMobile("898999");
custum.setCustPhone("110");
//实名一个联系人
LinkMan linkman =new LinkMan();
inkman.setLkm_gender("女");
linkman.setLkm_name("小王");
linkman.setLkm_phone("1314");
// 将给联系人指定联系人
linkman.setCustum(custum);
// 给客户添加联系人 两个实体之间需要互相指定
custum.getLinkmans().add(linkman);
//调用session 保存两个对象
session.save(custum);
session.save(linkman);
ts.commit();
}
(2) 上面的级联保存中两个对象需要互相指定,这样比较麻烦,我们可以用配置方法简化一些操作,在Custum.hbm.xml 中set标签中 加上cascade=“save-update”,这样配置以后代码如下:
@Test
public void add2()
{
Session session = HibernateUtils.getSession();
Transaction ts = session.beginTransaction();
Custum custum = new Custum();
custum.setCustName("百度");
custum.setCustLevel("普通用户");
custum.setCustSource("网络");
custum.setCustMobile("11111");
custum.setCustPhone("0000");
LinkMan linkman =new LinkMan();
linkman.setLkm_gender("男");
linkman.setLkm_name("小宏");
linkman.setLkm_phone("4433");
//主需要将联系人放到相应的客户中即可,不需要相互指定
custum.getLinkmans().add(linkman);
// 只需要保存 客户就可以实现将客户内的联系人也相应的保存下来
session.save(custum);
ts.commit();
}
2 一对多级联修改,代码如下:
@Test
public void updateTest()
{
Session session = HibernateUtils.getSession();
Transaction ts = session.beginTransaction();
// 根据Id 取出要要修改的联系人
LinkMan linkman = session.get(LinkMan.class,3);
//根据Id 取出联系人需要重新指定的客户
Custum custum = session.get(Custum.class,2);
//两者互相指定
linkman.setCustum(custum);
custum.getLinkmans().add(linkman);
ts.commit();
}
3 一对多级联删除,需要在Custum 中set属性中加上cascade 属性中加上delete,如果有多个值,可以用逗号隔开。调用session对象删除即可。