1.一对多

 1).首先创建两个实体类studeninfo.java跟studentxxb.java

    1)studentinfo.java表如图:

package model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set; public class studentinfo {
private Integer id;
private String name;
private String aihao;
private Date sjina;
private Set<studentxxb> studentxxbs=new HashSet<>();
public studentinfo() {
super();
}
public studentinfo(Integer id, String name, String aihao, Date sjina, Set<studentxxb> studentxxbs) {
super();
this.id = id;
this.name = name;
this.aihao = aihao;
this.sjina = sjina;
this.studentxxbs = studentxxbs;
}
@Override
public String toString() {
return "studentinfo [id=" + id + ", name=" + name + ", aihao=" + aihao + ", sjina=" + sjina + ", studentxxbs="
+ studentxxbs + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAihao() {
return aihao;
}
public void setAihao(String aihao) {
this.aihao = aihao;
}
public Date getSjina() {
return sjina;
}
public void setSjina(Date sjina) {
this.sjina = sjina;
}
public Set<studentxxb> getStudentxxbs() {
return studentxxbs;
}
public void setStudentxxbs(Set<studentxxb> studentxxbs) {
this.studentxxbs = studentxxbs;
}
}

  

   2)stdentxxb.java如下图:

    

package model;

import java.util.Date;

public class studentxxb {
private Integer id;
private String dhua;
private String diz;
private studentinfo studentinfos;
public studentxxb() {
super();
}
public studentxxb(Integer id, String dhua, String diz, studentinfo studentinfos) {
super();
this.id = id;
this.dhua = dhua;
this.diz = diz;
this.studentinfos = studentinfos;
}
@Override
public String toString() {
return "studentxxb [id=" + id + ", dhua=" + dhua + ", diz=" + diz + ", studentinfos=" + studentinfos + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDhua() {
return dhua;
}
public void setDhua(String dhua) {
this.dhua = dhua;
}
public String getDiz() {
return diz;
}
public void setDiz(String diz) {
this.diz = diz;
}
public studentinfo getStudentinfos() {
return studentinfos;
}
public void setStudentinfos(studentinfo studentinfos) {
this.studentinfos = studentinfos;
}
}

  2).两个实体类创建好了,接下来配置两个实体类的hbm.xml文件

      1).实体类studentinfo.java的配置(studentinfp-mapping.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="model.studentinfo" table="studentinfo">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="aihao" column="aihao"></property>
<property name="sjina" column="sjina"></property>
<set name="studentxxbs">
<key>
<column name="student_xxbs"></column>
</key>
<one-to-many class="model.studentxxb"/>
</set>
</class> </hibernate-mapping>

  

      2). 实体类studentxxb.java的配置(studentxxb-mapping.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="model.studentxxb" table="studentxxb">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="dhua" column="dhua"></property>
<property name="diz" column="diz"></property>
            
<many-to-one name="studentinfos" class="model.studentinfo" column="student_infos"></many-to-one> </class> </hibernate-mapping>

  3)接下来配置cfg.xml文件这里命名为(hibernate.cfg.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="model.studentinfo" table="studentinfo">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="aihao" column="aihao"></property>
<property name="sjina" column="sjina"></property>
<set name="studentxxbs">
<key>
<column name="student_xxbs"></column>
</key>
<one-to-many class="model.studentxxb"/>
</set>
</class> </hibernate-mapping>

  4)测试类 Testmain.java:

    

package test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import model.studentinfo;
import model.studentxxb; public class Testmain1 {
public static void main(String[] args) {
Session session=new Configuration().configure().buildSessionFactory().openSession();
Transaction tt=session.beginTransaction(); studentinfo studentinfo=new studentinfo();
studentinfo.setName("王胖子");
studentinfo.setAihao("爬山");
studentinfo.setSjina(new Date()); studentxxb xxb=new studentxxb();
xxb.setDiz("是是是");
xxb.setDhua("1111111");
studentinfo.getStudentxxbs().add(xxb);
xxb.setStudentinfos(studentinfo);
session.save(xxb);
session.save(studentinfo);
tt.commit();
session.close();
}
}

  5)控制台运行结果:

    

 

  6)数据库运行结果:

          

*先导包

    

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——一对多关系映射

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

  9. 03.Hibernate一对多关联

    前言:在域模型中,类与类之间最普遍的关系就是关联关系,在UML语言中关联关系是有方向的.在数据库中表与表之间也会有关联关系,本节介绍通过Hibernate映射一对多的关联关系,这是一种最普遍的关联关系 ...

随机推荐

  1. ASP.NET Core Authentication系列(一)理解Claim, ClaimsIdentity, ClaimsPrincipal

    前言 首先我们来看一下在ASP.NET时代,Authentication是如何使用的.下面介绍的是System.Web.Security.FormsAuthentication: // 登录 Syst ...

  2. NB-IoT窄带物联网技术的四大优势

      NB-IoT是指窄带物联网(Narrow Band -Internet of Things)技术,是IoT领域一个新兴的技术,支持低功耗设备在广域网的蜂窝数据连接,也被叫作低功耗广域网(LPWA) ...

  3. 串口服务器和modbus网关有什么不同

    串口服务器是什么? 串口服务器一般也会被称之为串口设备服务器,它是一种小型电子设备,可以将以太网IP/TCP数据包转换为RS232,RS485或RS422串口数据信号,反之亦然. Modbus网关是什 ...

  4. Spring中的BeanFactory与FactoryBean看这一篇就够了

    前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...

  5. scrapyd部署、使用Gerapy 分布式爬虫管理框架

    Scrapyd部署爬虫项目 GitHub:https://github.com/scrapy/scrapyd API 文档:http://scrapyd.readthedocs.io/en/stabl ...

  6. Reactor中的Thread和Scheduler

    目录 简介 Thread多线程 Schedule定时器 Schedulers工具类 publishOn 和 subscribeOn publishOn subscribeOn 简介 今天我们要介绍的是 ...

  7. Vue2.0源码分析

    Vue其实是用Function写的一个class: 1.通过一系列的函数给Vue.prototype上动态挂载方法和属性 2.通过initGlobalAPI(Vue)给Vue本身扩展全局API 数据驱 ...

  8. Socket listen 简要分析

    #include <sys/types.h> /* See NOTES */#include <sys/socket.h>int listen(int sockfd, int ...

  9. fcntl函数用法——操纵文件描述符状态

    fcntl函数:操纵文件描述符,改变已经打开的文件的属性int fcntl(int fd, int cmd, ... //arg  );cmd选项:一.复制文件描述符:F_DUPFD二.更改设置文件描 ...

  10. java版飞机大战代码

    @ 目录 前言 Plane PlaneStatus类 Power类 Gift Diji play类 over类 MainFrame主类 MyZiDan DijiZiDan Before 前言 很久之前 ...