OneToMany

public class OneToMany {

    @Test
public void testAdd1() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction(); Customer customer = new Customer();
customer.setCustName("顾客1");
customer.setCustLevel("vip");
customer.setCustSource("网络");
customer.setCustPhone("138");
customer.setCustMobile("999"); LinkMan linkman = new LinkMan();
linkman.setLkm_name("联系人1");
linkman.setLkm_gender("男");
linkman.setLkm_phone("109"); customer.getSetLinkMan().add(linkman);
linkman.setCustomer(customer); session.save(customer);
// session.save(linkman);
tx.commit();
System.out.println("====================");
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null)
session.close();
if (sessionFactory != null)
sessionFactory.close();
}
} @Test
public void testDelete1() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try {
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction(); Customer customer = session.get(Customer.class, 1);
if (null != customer) {
session.delete(customer);
}
tx.commit();
System.out.println("====================");
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
if (session != null)
session.close();
if (sessionFactory != null)
sessionFactory.close();
}
}
}

Customer

public class Customer {
//客户id
private Integer cid;
//客户名称
private String custName;
//客户级别
private String custLevel;
//客户来源
private String custSource;
//联系电话
private String custPhone;
//手机
private String custMobile; //在客户实体类里面表示多个联系人,一个客户有多个联系人
//hibernate要求使用集合表示多的数据,使用set集合
private Set<LinkMan> setLinkMan = new HashSet<LinkMan>(); public Set<LinkMan> getSetLinkMan() {
return setLinkMan;
} public void setSetLinkMan(Set<LinkMan> 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 Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 1 配置类和表对应
class标签
name属性:实体类全路径
table属性:数据库表名称
-->
<class name="k.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" inverse="true" cascade="save-update,delete">
<!-- 一对多建表,有外键
hibernate机制:双向维护外键,在一和多那一方都配置外键
column属性值:外键名称
-->
<key column="clid"></key>
<!-- 客户所有的联系人,class里面写联系人实体类全路径 -->
<one-to-many class="k.entity.LinkMan"/>
</set>
</class>
</hibernate-mapping>

LinkMan

public class LinkMan {
private Integer lkm_id; // 联系人编号(主键)
private String lkm_name;// 联系人姓名
private String lkm_gender;// 联系人性别
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_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;
}
}

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>
<!-- 1 配置类和表对应
class标签
name属性:实体类全路径
table属性:数据库表名称
-->
<class name="k.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> <!-- 表示联系人所属客户
name属性:因为在联系人实体类使用customer对象表示,写customer名称
class属性:customer全路径
column属性:外键名称
-->
<many-to-one name="customer" class="k.entity.Customer" column="clid"></many-to-one>
</class>
</hibernate-mapping>

HibernateUtils

public class HibernateUtils {

    static Configuration cfg = null;
static SessionFactory sessionFactory = null;
//静态代码块实现
static {
//加载核心配置文件
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
} //提供返回与本地线程帮的session的方法
public static Session getSessionobject() {
return sessionFactory.getCurrentSession();
} //提供方法返回sessionFactory
public static SessionFactory getSessionFactory() {
return sessionFactory;
} public static void main(String[] args) { }
}

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:///hibernate_day01</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root3306</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
让hibernate框架识别不同数据库的自己特有的语句
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 第三部分: 把映射文件放到核心配置文件中 必须的-->
<!--<mapping resource="k/entity/User.hbm.xml"/>-->
<mapping resource="k/entity/Customer.hbm.xml"/>
<mapping resource="k/entity/LinkMan.hbm.xml"/>
</session-factory>
</hibernate-configuration>

【Hibernate 一对多】的更多相关文章

  1. Hibernate一对多配置

    刚刚学习了Hibernate框架的基础知识,下面我来说说关于Hibernate一对多的配置 首先是大配置 连接数据库 用户名 和密码 能和小配置连接 部门小配置: 员工小配置: 部门实体类 员工实体类 ...

  2. hibernate 一对多双向关联 详解

    一.解析: 1.  一对多双向关联也就是说,在加载班级时,能够知道这个班级所有的学生. 同时,在加载学生时,也能够知道这个学生所在的班级. 2.我们知道,一对多关联映射和多对一关联映射是一样的,都是在 ...

  3. Hibernate一对多OnetoMany

    ------------------------Hibernate一对多OnetoMany 要点: 配置在一端. 1.如果是单向关联,即只在一端配置OneToMany,多端不配置ManyToOne.则 ...

  4. Hibernate一对多单向关联和双向关联映射方法及其优缺点 (待续)

    一对多关联映射和多对一关联映射实现的基本原理都是一样的,既是在多的一端加入一个外键指向一的一端外键,而主要的区别就是维护端不同.它们的区别在于维护的关系不同: 一对多关联映射是指在加载一的一端数据的同 ...

  5. Hibernate一对多操作

    --------------------siwuxie095 Hibernate 一对多操作 以客户和联系人为例,客户是一,联系人是多 即 一个客户里面有多个联系人,一个联系人只能属于一个客户 注意: ...

  6. Java进阶知识10 Hibernate一对多_多对一双向关联(Annotation+XML实现)

    本文知识点(目录): 1.Annotation 注解版(只是测试建表)    2.XML版 的实现(只是测试建表)    3.附录(Annotation 注解版CRUD操作)[注解版有个问题:插入值时 ...

  7. Java进阶知识09 Hibernate一对多单向关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.在一的一方加Set 1.2.创建Customer类和Order类 package com.shore.model; import java.util.Hash ...

  8. Hibernate—— 一对多 和 多对多关联关系映射(xml和注解)总结(转载)

    One to Many 映射关系 多对一单向外键关联(XML/Annotation) 一对多单向外键关联(XML/Annotation) 懒加载和积极加载 一对多双向外键关联(XML/Annotati ...

  9. Hibernate一对多(多对一)关联关系

    上一篇博文总结了 Hibernate 的一对一的关联关系, 包括基于主键的单向一对一, 基于外键的单向一对一, 基于外键的双向一对一. 下面咱们说一下 Hibernate 的一对多关联关系. 其实一对 ...

  10. 菜鸟学习Hibernate——一对多关系映射

    Hibernate中的关系映射,最常见的关系映射之一就是一对多关系映射例如学生与班级的关系,一个班级对应多个学生.如图: Hibernate中如何来映射这两个的关系呢? 下面就为大家讲解一下: 1.创 ...

随机推荐

  1. 每天进步一点点------Allegro 铺铜详解

    铺铜在设计PCB板时很重要,为了加深理解,笔者写下这篇学习的过程. 首先要理解什么是正片和负片,结合网上的资料来理解一下: 正片实际就是能在底片上能看到的就是存在的 负片实际上就是在底片看到的就是不存 ...

  2. Codeforces Global Round 3:B. Born This Way

    Born This Way原文链接:[传送门] 题目大意:潇洒哥想乘坐飞机从A地到达C地,但是没有直达的航班,在A地和B地之间有一个可以中转的航班B,潇洒哥想早点到达C地(有航班就坐),但是很不幸他得 ...

  3. RegExp-named captured groups(命名分组捕获)

    console.log('2020-01-23'.match(/(\d{4})-(\d{2})-(\d{2})/)) const t = '2020-01-23'.match(/(?<year& ...

  4. MySQL-THINKPHP 商城系统一 商品模块的设计

    在此之前,先了解下关于SPU及SKU的知识 SPU是商品信息聚合的最小单位,是一组可复用.易检索的标准化信息的集合,该集合描述了一个产品的特性.通俗点讲,属性值.特性相同的商品就可以称为一个SPU. ...

  5. KMP字符串匹配算法详解

    KMP算法利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.具体实现就是实现一个next()函数,函数本身包含了模式串的局部匹配信息.时间复杂度O(m+n). Next()函数 ...

  6. FreeRTOS学习笔记3:内核控制及开启调度器

    内核控制函数API 应用层中不会用到taskYIELD() //任务切换.会自动切换当前就绪表里优先级最高的任务 临界区 //不能被打断的代码段任务中进入临界区任务中退出临界区中断服务进入临界区中断服 ...

  7. bugku 本地包含

    本地包含 题目信息 地址:http://123.206.87.240:8003/ <?php include "flag.php"; $a = @$_REQUEST['hel ...

  8. python 实现 md文档自动编号

    目录 1. 原理 2. 运行方法 3. 效果 4. 代码 1. 原理 正则匹配对相应字符串进行替换 2. 运行方法 python md_convert.py [a.md, b.md,...] # 转换 ...

  9. Go性能调优

    文章引用自   Go性能调优 在计算机性能调试领域里,profiling 是指对应用程序的画像,画像就是应用程序使用 CPU 和内存的情况. Go语言是一个对性能特别看重的语言,因此语言中自带了 pr ...

  10. IDE - IDEA - 快捷键整理 - 01. Navigation

    1. 概述 工具的熟练程度, 会决定工作效率 总共也就 140 条左右吧 需要讲解吗? 2. ref 1. idea 自带的 ReferenceCard.pdf 3. keymap 1. 文件移动 C ...