一对多、多对一

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. 酷!微软发布新终端工具,Win 10 将自带 Linux 内核

    原创:技术最前线(id:TopITNews) 北京时间 5 月 7 日,2019 年微软 Build 开发者大会在雷德蒙德召开.今年大会上亮点很多,本文汇总一些和开发者相关的内容. 1. Window ...

  2. DP(动态规划)总结

    前言 动态规划是很重要的一个知识点,大大小小的比赛总会有一两道DP题,足以说明动态规划的重要性. 动态规划主要是思想,并没有固定的模板,那么,怎么判断题目是不是动态规划呢? DP题一般都会满足三个条件 ...

  3. django下创建多个app,如何设置每个app的urls

    1.创建第二个app 假设我们项目P下面已经有了一个默认的app,名字是app1.现在我想创建第二个app,名字时app2. 进入pychram下的Terminal中,运行命令: python man ...

  4. spring session cpu占用过高

      集成spring session很简单,只需几行代码即可. @Configuration @EnableRedisHttpSession public class SessionConfig { ...

  5. Ubuntu修改文件权限以及更换文件所有者

    参数 -R 用来递归实现更改所有子文件和子目录的权限. 1.利用chmod修改权限: 对Document/目录下的所有子文件与子目录执行相同的权限变更: chmod -R 700 文件名 700是变更 ...

  6. UOJ348 WC2018 州区划分 状压DP、欧拉回路、子集卷积

    传送门 应该都会判欧拉回路吧(雾 考虑状压DP:设\(W_i\)表示集合\(i\)的点的权值和,\(route_i\)表示点集\(i\)的导出子图中是否存在欧拉回路,\(f_i\)表示前若干个城市包含 ...

  7. VsCode中好用的git源代码管理插件GitLens

    1.在插件tab搜索GitLens 2.安装成功后将光标移至代码行即会显示代码编写者 3.在VsCode左侧菜单栏,点击GitLens图标即可查看History,也可以查看全部的日志 4.查看上下pu ...

  8. Spring AOP创建AroundAdvice实例

    AroundAdvice 1.在方法之前和之后来执行相应的操作 2.实现MethodInterceptor接口 接口文件: public interface IHello { public void ...

  9. java之struts2之异常处理

    1.在应用项目中,异常的出现时很正常的.而且项目上线后发生异常也很正常的.那么需要对这些异常有相应的处理机制,以便客户能够看你到更加友好的界面.Struts2中提供了异常处理机制. 2.Struts中 ...

  10. js计算结果不精确问题解决--math.js的使用

    最近在做订单相关的一个功能,涉及到金额的计算,有人建议,将计算全部抛给后端来做吧,前端就不需要再维护一套算法了,话说的在理,但是呢,想想用户体验,单价*数量=金额,当用户改变一个数量时,用户都口算出来 ...