返回

Hibernate 学习笔记 之 一对多关系 及其 级联操作

发布时间:2022-10-24 18:52:10 350
# java# mysql# 数据库# sql# 信息

一、一对多关系

Hibernate 学习笔记 之 一对多关系 及其 级联操作_外键

Customer.java
/**
* Created by Donald on 2016/11/19.
*/
public class Customer
private Integer cid;
private String custName;
private String custLevel;
private String custSource; //客户来源
private String custPhone; //联系电话
private String custMobile; //手机

/**
* 在客户实体类里面表示多个联系人,一个客户有多个联系人
* hibernate要求使用集合表示多的数据,使用set集合
* */
private Set setLinkMan = new HashSet();

public Set getSetLinkMan() {
return setLinkMan;
}

public void setSetLinkMan(Set setLinkMan) {
this.setLinkMan = setLinkMan;
}

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;
}
}
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- 1.配置类和表对应
        class标签
        name属性,实体类
        table属性,数据表名称
        -->
    <class name="com.yyf.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结合名称
        -->
        <set name="setLinkMan" cascade="save-update,delete">
            <!--
                一对多建表,有外键
                hibernate机制,双向维护外键,在一和多那一方都配置外键
                column属性值就是外键名称
            -->
            <key column="clid"></key>
            <!-- 客户所有的联系人,class里面写联系人实体类全路径 -->
            <one-to-many  class="com.yyf.entity.LinkMan"
        </set>
    </class>
</hibernate-mapping>
LinkMan.java
/**
* Created by Donald on 2016/11/19.
*/
public class LinkMan
private Integer lkm_id; //联系人编号(主键)
private String lkm_name; //联系人姓名
private String lkm_gander; //联系人性别
private String lkm_phone; //联系人办公电话

/**
* 在联系人实体类里面表示所属客户,
* 一个联系人只能属于一个客户
*/
private Customer customer;

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

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_gander() {
return lkm_gander;
}

public void setLkm_gander(String lkm_gander) {
this.lkm_gander = lkm_gander;
}

public String getLkm_phone() {
return lkm_phone;
}

public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
}
LinkMan.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- 1.配置类和表对应
        class标签
        name属性,实体类
        table属性,数据表名称
        -->
    <class name="com.yyf.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_gander" column="lkm_gander"></property>
        <property name="lkm_phone" column="lkm_phone"></property>


        <!-- 表示联系人所属客户
            name属性,因为在联系人实体类使用customer对象表示,写customer名称
            class属性: customer全路径
            column属性:外键名称
        -->
        <many-to-one name="customer" class="com.yyf.entity.Customer" column="clid"</many-to-one>
    </class>
</hibernate-mapping>
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://127.0.0.1:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">admin</property>

        <!--第二部分 配置hibernate信息-->
        <!-- 输出底层sql语句-->
        <property name="hibernate.show_sql">true</property>
        <!-- 输出底层sql语句格式-->
        <property name="hibernate.format_sql">true</property>
        <!-- hibernate帮创建表,需要配置之后
            update: 如果已经有这个表,则更新,如果没有,创建
        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 配置数据库方言
            在mysql里面实现分页 关键字 limit,只能使用在mysql里
            在oracle数据库,实现分页rownum
            让hiberna框架识别不同数据库的语句
        -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 在hibernate核心配置文件中配置-->
        <property name="hibernate.current_session_context_class">thread</property>

        <!--第三部分 把映射文件放到核心配置文件中-->
        <mapping resource="com/yyf/entity/Customer.hbm.xml"
        <mapping resource="com/yyf/entity/LinkMan.hbm.xml"
    </session-factory>
</hibernate-configuration>

 


首先

为方便操作,减少代码量,可以在一对多中的“一”的Xxx.hbm.xml里进行配置,如下图:

Hibernate 学习笔记 之 一对多关系 及其 级联操作_xml_02

二、级联操作 — 更新

/**
* 简化写法
* 一般根据客户添加联系人
* (1)在客户映射文件中进行配置
* 在客户映射文件里面set标签进行配置
*
* (2)创建客户和联系人对象,
* 只需要把联系人放在客户里面就可以了,
* 最终只需要保存客户就可以了。
*/

@Test
public void testAdd2() {
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_gander("男");
linkMan.setLkm_phone("911");

//2.把联系人放在客户里面
customer.getSetLinkMan().add(linkMan);

//3.保存客户
session.save(customer);


tx.commit();
}catch

三、级联操作 — 删除

/**
* 1. 删除某个客户,把客户里面所有的联系人删除
*
* (1)使用属性cascade属性值delete
*
* (2)在代码中直接删除客户
* 根据id查询对象,调用session里面delete方法删除
*/
@Test
public void testDelete() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();

//1.根据id查询客户对象
Customer customer = session.get(Customer.class, 2);
//2.调用方法删除
session.delete(customer);


tx.commit();
}catch

四、操作 — inverse

Hibernate 学习笔记 之 一对多关系 及其 级联操作_xml_03

若不加这个属性则在更新外键时候,则会双向维护,即更新两次,造成不必要的浪费

/**
* inverse属性
*
* (1)因为hibernate双向维护外键,在客户和联系人里面都需要
* 维护外键,修改客户时候修改一次外键,修改联系人时候也修改一次外键,造成效率问题
*
* (2)解决方式:让其中的一方不维护外键
* - 一对多里面,让其中一方放弃外键维护
* - 一个国家有总统。国家有很多人,总统不能认识国家所有人,国家所有人可以认识总统
*
* (3)具体实现:
* 在放弃关系维护映射文件中,进行配置,在set标签上使用inverse
*/
@Test
public void testUpdate() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();

//1.根据id查询客户对象
Customer customer = session.get(Customer.class, 2);
LinkMan linkMan = session.get(LinkMan.class, 1);

customer.getSetLinkMan().add(linkMan);
linkMan.setCustomer(customer);

tx.commit();
}catch

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
拓扑排序 2022-10-24 18:08:01