PringData JPA一对多多对一多对多关联
一、一对多、多对一
1、Country实体类

2、City实体类

3、CountryDao层

4、CityDao层

5、Controller
package com.zn.controller; import com.zn.dao.CityDao;
import com.zn.dao.CountryDao;
import com.zn.entity.City;
import com.zn.entity.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class CountryController {
@Autowired
CountryDao countryDao;
CityDao cityDao; //级联添加
@RequestMapping("/OneToMany")
@ResponseBody
public String AddCountry(){
Country country1=new Country();
country1.setCountry_name("中国");
City city1=new City();
city1.setCity_name("中国香港");
City city2=new City();
city2.setCity_name("中国台湾"); //维护国家与城市的一对多关系
country1.getCitys().add(city1);
country1.getCitys().add(city2);
countryDao.save(country1);
return "SUCCESS";
} //关联查询
@RequestMapping("/getCountry")
@ResponseBody
public Object getCountry(){
return countryDao.findAll();
} //级联删除
@RequestMapping("/deleteCountry")
@ResponseBody
public String deleteCountry(){
//检索国家实体
Country one=countryDao.getOne(5);
countryDao.delete(one);
return "SUCCESS";
} //由城市到国家的关联查询
@RequestMapping("/getCity")
@ResponseBody
public Object getCity(){
return countryDao.findAll();
} }
二、多对多
1、TStudent实体类

2、Teacher实体类

3、Stu_TeaDao

4、Tea_StuDao

5、Controller
package com.zn.controller; import com.zn.dao.Stu_TeaDao;
import com.zn.dao.Tea_StuDao;
import com.zn.entity.TStudent;
import com.zn.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.Arrays;
import java.util.List; @Controller
public class Stu_TeaController { @Autowired
Stu_TeaDao stu_teaDao; @Autowired
Tea_StuDao tea_stuDao; //添加学生和老师
@RequestMapping("/addstu")
@ResponseBody
public String addstu(){
TStudent student1=new TStudent("张三");
TStudent student2=new TStudent("李四");
TStudent student3=new TStudent("王五"); Teacher teacher1=new Teacher("小王子");
student1.getTeachers().add(teacher1);
student2.getTeachers().add(teacher1);
student3.getTeachers().add(teacher1); stu_teaDao.saveAll(Arrays.asList(student1,student2,student3));
return "SUCCESS";
} //多对多添加老师
@RequestMapping("/addTea")
@ResponseBody
public String addTea(){
Teacher teacher=new Teacher("王老师");
List<TStudent> all = stu_teaDao.findAll();
teacher.getStudents().addAll(all);
tea_stuDao.save(teacher);
return "SUCCESS";
} //多对多关联查询(慎用!!死循环!!)
@RequestMapping("/getTea")
@ResponseBody
public Object getTea(){
return tea_stuDao.getOne(1);
}
}

PringData JPA一对多多对一多对多关联的更多相关文章
- 【Jpa hibernate】一对多@OneToMany,多对一@ManyToOne的使用
项目中使用实体之间存在一对多@OneToMany,多对一@ManyToOne的映射关系,怎么设置呢? GitHub地址:https://github.com/AngelSXD/myagenorderd ...
- JPA 一对多、多对一注解
1. @OneToMany @OneToMany 是属性或方法级别的注解,用于定义源实体与目标实体是一对多的关系. 参数 类型 描述 targetEntity Class 源实体关联的目标实体类型,默 ...
- JPA 系列教程7-双向多对多
双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...
- django 中models表的多对一,多对多的理解
django 表的理解 好处:设计的好,会清晰,易于理解和维护,后期开发事半功倍,一目了然. 1. 一对一的表,两表的属性实际上完全可以合并成一个表,共用一个主键即可: 2. 一对多的表,可以设中间关 ...
- JPA一对多关联
关于JPA一对多关联这里使用Order与OrderItem来模拟.一个Order可以关联多个OrderItem,而一个OrderItem只能关联一个Order.Order与OrderItem是一对多的 ...
- Python sqlalchemy orm 多对多外键关联
多对多外键关联 注:使用三张表进行对应关联 实现代码: # 创建3个表 配置外键关联 # 调用Column创建字段 加类型 from sqlalchemy import Table, Column, ...
- JPA一对多和多对一关系
1-m:多的一方为关系维护端,关系维护端负责外键纪录的更新,关系被维护端没有权力更新外键纪录. 维护端注解 @OneToMany(cascade = { CascadeType.PERSIST, Ca ...
- jpa 一对多and 多对一
配置: <?xml version="1.0" encoding="UTF-8"?> <persistence version="2 ...
- JPA 一对一 一对多 多对一 多对多配置
1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...
随机推荐
- MIT线性代数:16.投影矩阵和最小二乘
- mysql约束条件
约束条件 (测试markdown 编辑器专用) null 允许为空 not null 不允许为空 key 键值类型 default 设置默认类型,缺省值为NULL extra 额外设置 CREATE ...
- python 爬取猫眼电影top100数据
最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...
- 苹果审核ipv6海外解决思路-About APP Store
原始简书文章地址(也是我自己的) 首先声明,一我不负责涉及你们内部服务器. 二是好好读文章,别人能过,你们也能过 苹果6月1日出的IPV6协议阻碍了国内大多数积极开发者,我司也不外乎,经过三次被拒后, ...
- HTML和css面试题:内容转载
1.常见的块级元素 内联元素 div -最常用的块级元素 dl - 和dt-dd 搭配使用的块级元素 form - 交互表单 h1 -h6- 大标题 hr - 水平分隔线 ol – 有序列表 p - ...
- 如何查看当前linux服务器是否支持虚拟化
[root@localhost ~]# grep -E '(svm|vmx)' /proc/cpuinfo 或者: [root@localhost ~]# cat /proc/cpuinfo 找到fl ...
- .NET Core3.0 EF 连接 MySql
一:创建项目 添加 csproj (或者直接NuGet 引用) <ItemGroup> <PackageReference Include="Microsoft.Entit ...
- nyoj 206-矩形的个数 (a*b*(a+1)*(b+1)/4)
206-矩形的个数 内存限制:64MB 时间限制:1000ms 特判: No 通过数:16 提交数:37 难度:1 题目描述: 在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1* ...
- go中的关键字-defer
1. defer的使用 defer 延迟调用.我们先来看一下,有defer关键字的代码执行顺序: func main() { defer func() { fmt.Println("1号输出 ...
- 【编程题与分析题】Javascript 之继承的多种实现方式和优缺点总结
[!NOTE] 能熟练掌握每种继承方式的手写实现,并知道该继承实现方式的优缺点. 原型链继承 function Parent() { this.name = 'zhangsan'; this.chil ...