<?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>
<!-- 1 配置类和表对应
class标签
name属性:实体类全路径
table属性:数据库表名称
-->
<class name="com.hlg.entity.Customer" table="t_customer">
<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标签表示所有联系人
set标签里面有name属性:
属性值写在客户实体类里面表示联系人的set集合名称
inverse属性默认值:false不放弃关系维护
true表示放弃关系维护
-->
<set name="setLinkMan" cascade="save-update">
<!-- 一对多建表,有外键
hibernate机制:双向维护外键,在一和多那一方都配置外键
column属性值:外键名称
-->
<key column="clid"></key>
<!-- 客户所有的联系人,class里面写联系人实体类全路径 -->
<one-to-many class="com.hlg.entity.LinkMan"/>
</set>
</class>
</hibernate-mapping>
package com.hlg.hibernatetest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import com.hlg.entity.Customer;
import com.hlg.entity.LinkMan;
import com.hlg.utils.HibernateUtils;
public class HibernateOnetoMany {
//一对多 级联保存
@Test
public void testOneToMany(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
// 添加一个客户,为这个客户添加一个联系人
//1 创建客户和联系人对象
Customer customer = new Customer();
customer.setCustName("传智播客");
customer.setCustLevel("vip");
customer.setCustSource("网络");
customer.setCustPhone("110");
customer.setCustMobile("999");
LinkMan linkman = new LinkMan();
linkman.setLkm_name("lucy");
linkman.setLkm_gender("男");
linkman.setLkm_phone("911");
//2 在客户表示所有联系人,在联系人表示客户
// 建立客户对象和联系人对象关系
//2.1 把联系人对象 放到客户对象的set集合里面
customer.getSetLinkMan().add(linkman);
//2.2 把客户对象放到联系人里面
linkman.setCustomer(customer);
//3 保存到数据库
session.save(customer);
session.save(linkman);
//提交事务
tx.commit();
}catch(Exception e){
//回滚事务
tx.rollback();
}finally{
//关闭
session.close();
sessionFactory.close();
}
}
//一对多 级联保存 简化写法
@Test
public void testOneToMany2(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
// 添加一个客户,为这个客户添加一个联系人
//1 创建客户和联系人对象
Customer customer = new Customer();
customer.setCustName("迅雷");
customer.setCustLevel("普通客户");
customer.setCustSource("网络");
customer.setCustPhone("110");
customer.setCustMobile("999");
LinkMan linkman = new LinkMan();
linkman.setLkm_name("小雷");
linkman.setLkm_gender("男");
linkman.setLkm_phone("911");
//2 把联系人放到客户里面
customer.getSetLinkMan().add(linkman);
//3 保存到数据库
session.save(customer);
//提交事务
tx.commit();
}catch(Exception e){
//回滚事务
tx.rollback();
}finally{
//关闭
session.close();
sessionFactory.close();
}
}
}