一对多、多对一

Country类

@Entity
@Table(name = "Country")
public class Country {
@Id
//sequence identity
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer countryid;
private String country;
//mappedBy:指定由谁维护关联关系(设置的是关联对象的属性名)
@OneToMany(cascade = CascadeType.ALL,mappedBy = "country")
//@JoinColumn(name="countryid")
private List<City> citys=new ArrayList<>(); //关联属性

City类

@Entity
@Table(name = "City")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cityid;
private String cityname;
@ManyToOne(cascade = CascadeType.ALL)
private Country country;

CountryDao层

public interface CountryDao extends JpaRepository<Country,Integer>{
}

CityDao层

public interface CityDao extends JpaRepository<City,Integer>{
}

CountryController

@Controller
public class CountryController { @Autowired
CountryDao dao; @Autowired
CityDao dao1; @Autowired
StudentDao dao2; @Autowired
TeacherDao dao3; //级联增加
@RequestMapping("/onetomany")
public String onetomany(){
Country c1=new Country();
c1.setCountry("中国万岁 ChinaNo.1");
City ct1=new City();
ct1.setCityname("中国香港");
City ct2=new City();
ct2.setCityname("中国台湾"); //维护国家与城市的一对多关系
c1.getCitys().add(ct1);
c1.getCitys().add(ct2); dao.save(c1);
return "success";
} //关联查询
@RequestMapping("/getcountry")
@ResponseBody
public Object get(){
return dao.findAll();
} //级联删除
@RequestMapping("/deleteCountry")
public String delete(){
//检索国家实体
Country one = dao.getOne();
dao.delete(one);
return "success";
} //由城市到国家的关联查询
@RequestMapping("/getCity")
@ResponseBody
public Object getCity(){
return dao1.findAll();
} //添加学生和老师
@RequestMapping("/saveStudent")
public String save(){
Student student1=new Student("利亚东哥");
Student student2=new Student("玉姐");
Student student3=new Student("雄哥"); Teacher t1=new Teacher("山间的风");
student1.getTeachers().add(t1);
student2.getTeachers().add(t1);
student3.getTeachers().add(t1);
dao2.saveAll(Arrays.asList(student1,student2,student3)); return "success"; } @RequestMapping("/saveTeacher")
public String saveTeacher(){
Teacher t1=new Teacher("帅的离谱");
List<Student> all = dao2.findAll();
t1.getStudents().addAll(all);
dao3.save(t1);
return "success";
} @RequestMapping("/getTeacher")
@ResponseBody
public Object getTeacher(){
return dao3.getOne();
}
}

多对多

Teacher

@Entity
@Table(name="teacherinfo")
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer teacherid;
private String teachername;
@ManyToMany(cascade = CascadeType.ALL)
//mappedby属性绝对不能再存在@JoinTable和@JoinColumn注解时使用
@JoinTable(name="student_teacher",joinColumns =@JoinColumn(name="teacherid") ,
inverseJoinColumns =@JoinColumn(name="studentid") )
private List<Student> students=new ArrayList<>();

Student

@Entity
@Table(name = "studentinfo")
public class Student {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
private Integer sex;
@Column(name = "stu_name")

TeacherDao

public interface TeacherDao extends JpaRepository<Teacher,Integer> {
}

StudentDao

public interface StudentDao extends JpaRepository<Student,Integer>{
}

StudentController

@Controller
public class StudentController{ @Autowired
StudentDao studentDao; @Autowired
TeacherDao teacherDao; //添加学生和老师
@RequestMapping("/addstu")
@ResponseBody
public String addstu(){
Student student1=new Student("丽丽");
Student student2=new Student("明明");
Student student3=new Student("安安"); Teacher teacher1=new Teacher("筱丽");
student1.getTeachers().add(teacher1);
student2.getTeachers().add(teacher1);
student3.getTeachers().add(teacher1); studentDao.saveAll(Arrays.asList(student1,student2,student3));
return "SUCCESS";
} //多对多添加老师
@RequestMapping("/addDom")
@ResponseBody
public String addDom(){
Teacher teacher=new Teacher("李老师");
List<Student> all = studentDao.findAll();
teacher.getStudents().addAll(all);
teacherDao.save(teacher);
return "SUCCESS";
} //多对多关联查询(慎用!!死循环!!)
@RequestMapping("/getDom")
@ResponseBody
public Object getDom(){
return teacherDao.getOne();
}
}

spring-data-jpa一对多多对一多对多关联的更多相关文章

  1. spring data jpa 一对多查询

    在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...

  2. Spring Data JPA 学习记录1 -- 单向1:N关联的一些问题

    开新坑 开新坑了(笑)....公司项目使用的是Spring Data JPA做持久化框架....学习了一段时间以后发现了一点值得注意的小问题.....与大家分享 主要是针对1:N单向关联产生的一系列问 ...

  3. Spring Data Jpa一对多单向映射

    /** @author StormMaybin @date 2017-01-17 */ 生命不息,奋斗不止! 一对多映射关系 在JPA中,用@OneToMany来标识一对多的关系.实现一对多的单向关联 ...

  4. Spring Data JPA one to one 共享主键关联

    /** * Created by xiezhiyan on 17-9-13. */@Entitypublic class Token { @Id @Column(name = "store_ ...

  5. spring data jpa 多对多查询

    package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...

  6. spring data jpa 多对多 ManyToMany

    环境搭建 源码地址:gitee:https://gitee.com/ytfs-dtx/JPA 导入依赖 <properties> <spring.version>5.2.5.R ...

  7. Spring Data JPA 初体验

    一,JPA相关的概念 JPA概述 全称是:JavaPersistence API.是SUN公司推出的一套基于ORM的规范. Hibernate框架中提供了JPA的实现. JPA通过JDK 5.0注解或 ...

  8. Spring Data JPA实体详解

    1. Spring Data JPA实体概述 JPA提供了一种简单高效的方式来管理Java对象(POJO)到关系数据库的映射,此类Java对象称为JPA实体或简称实体.实体通常与底层数据库中的单个关系 ...

  9. Spring Boot + Spring Data JPA + PostgreSQL

    最近在用Java重写之前实习生用.netcore写的微信后台应用. 规定用Spring Boot框架,PostgreSQL数据库.之前一直习惯于基于XML的Spring app,也没用过Postgre ...

  10. Spring Data Jpa系列教程--------实体解析和关联关系

    Spring Data Jpa是基于HIbernate开发的,所以建立实体建的实体和映射关系需要好好好的去了解一下,本文有以下内容,实体管理器介绍,实体与数据库表的映射介绍,关联关系(一对多,多对多) ...

随机推荐

  1. 【IDEA使用技巧】(3) —— IntelliJ IDEA Maven配置

    1.IntelliJ IDEA Maven配置 1.1. Maven介绍与下载 Maven是一个项目管理工具,使用它能对Java项目中的jar包进行管理与项目构建,很好地解决了传统项目使用导包的方式管 ...

  2. 【LEETCODE】49、数组分类,简单级别,题目:566,1089

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  3. Modelsim——显示状态机名称的方法

    方法在本人博客<状态机的Verilog写法>已经写明,为了方便查看,特意拎出来. 方法1: Testbench 设计文件含有状态机时,对应的仿真文件testbench里增加一段参数转ASC ...

  4. 按键消抖——task任务和仿真平台搭建

    一.按键抖动原理 按键抖动原理:按键存在一个反作用弹簧,因此当按下或者松开时均会产生额外的物理抖动,物理抖动会产生电平的抖动. 消抖方法:一般情况下,抖动的总时间会持续20ms以内,按下按键后,等20 ...

  5. spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件

    spring cloud  读取 配置文件属性值 1.bean @Data public class LocalFileConfig { /** * 文件存储地址 */ private String ...

  6. linux BufferedImage.createGraphics()卡住不动

    项目应用服务器tomcat7,在开发(windows).测试环境(linux 64bit)均正常.在生产环境(linux 64bit)一直启动不起来,也没有报错. 最终定位问题:执行到buffered ...

  7. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  8. python day4 元组/字典/集合类知识点补充

    目录 python day4 元组/字典/集合类知识点补充 1. 元组tuple知识点补充 2. 字典dict的知识点补充 3. 基本数据类型set 4. 三元运算,又叫三目运算 5. 深复制浅复制 ...

  9. Android studio来开发移动App--SQA计划和系统测试规程

    概述 团队分工 产品需求 团队合作 每日例会 思维导图 UML 产品代码 团队分工 成员:刘鹏芝,罗樟,王小莉,沈兴艳,徐棒,彭康明,胡广键 产品用户:王小莉 需求规约:彭康明,罗樟 UML:刘鹏芝, ...

  10. iOS原生与H5交互

    一.WKWebView WKWebView 初始化时,有一个参数叫configuration,它是WKWebViewConfiguration类型的参数,而WKWebViewConfiguration ...