spring-data-jpa一对多多对一多对多关联
一对多、多对一
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一对多多对一多对多关联的更多相关文章
- spring data jpa 一对多查询
在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. pa ...
- Spring Data JPA 学习记录1 -- 单向1:N关联的一些问题
开新坑 开新坑了(笑)....公司项目使用的是Spring Data JPA做持久化框架....学习了一段时间以后发现了一点值得注意的小问题.....与大家分享 主要是针对1:N单向关联产生的一系列问 ...
- Spring Data Jpa一对多单向映射
/** @author StormMaybin @date 2017-01-17 */ 生命不息,奋斗不止! 一对多映射关系 在JPA中,用@OneToMany来标识一对多的关系.实现一对多的单向关联 ...
- Spring Data JPA one to one 共享主键关联
/** * Created by xiezhiyan on 17-9-13. */@Entitypublic class Token { @Id @Column(name = "store_ ...
- spring data jpa 多对多查询
package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...
- spring data jpa 多对多 ManyToMany
环境搭建 源码地址:gitee:https://gitee.com/ytfs-dtx/JPA 导入依赖 <properties> <spring.version>5.2.5.R ...
- Spring Data JPA 初体验
一,JPA相关的概念 JPA概述 全称是:JavaPersistence API.是SUN公司推出的一套基于ORM的规范. Hibernate框架中提供了JPA的实现. JPA通过JDK 5.0注解或 ...
- Spring Data JPA实体详解
1. Spring Data JPA实体概述 JPA提供了一种简单高效的方式来管理Java对象(POJO)到关系数据库的映射,此类Java对象称为JPA实体或简称实体.实体通常与底层数据库中的单个关系 ...
- Spring Boot + Spring Data JPA + PostgreSQL
最近在用Java重写之前实习生用.netcore写的微信后台应用. 规定用Spring Boot框架,PostgreSQL数据库.之前一直习惯于基于XML的Spring app,也没用过Postgre ...
- Spring Data Jpa系列教程--------实体解析和关联关系
Spring Data Jpa是基于HIbernate开发的,所以建立实体建的实体和映射关系需要好好好的去了解一下,本文有以下内容,实体管理器介绍,实体与数据库表的映射介绍,关联关系(一对多,多对多) ...
随机推荐
- 自定义 Word 默认的 Normal.dotm 模板、更改 Word 默认字体、更改 Word 默认样式(16)
1. 引言 以Office 2016为例. 有没有遇见这样的问题: 每次新建一个 Word 空白文档打开后字体默认是等线,段落默认是单倍行距,默认标题也不是自己想要的样式,等一系列问题.每次打开都要调 ...
- mysql中数据表记录的增删查改(2)
select `数据表.字段1`, group_concat(`数据表.字段2`) from `数据表` group by `数据表.字段1` order by `数据表.字段1` desc; sel ...
- JOIN的区别
CREATE TABLE `j1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `c1` varchar(20) NOT NULL DEFAU ...
- 2. RDD编程
2.1 编程模型 在Spark中,RDD被表示为对象,通过对象上的方法调用来对RDD进行转换.经过一系列的transformations定义RDD之后,就可以调用actions触发RDD的计算,act ...
- YARN-HA高可用集群搭建
YARN-HA配置 1. YARN-HA工作机制 1.1 官方文档:http://hadoop.apache.org/docs/r2.7.2/hadoop-yarn/hadoop-yarn-site/ ...
- SharePoint中用Power shell命令修改文档的创建时间
第一步:pnp组件连接到SharePointConnect-PnpOnline -url 网站地址 第二步:查出文档库及文档库下所有的文件 Get-PnPListItem -List 文档库名称 第三 ...
- ASP.NET MVC+Entity Framework code first 迁移
再来一张,选择 MVC 模版,其他的没选过,不会用 =_=!! 身份验证用个人用户账户,这个是为了偷懒,话说 ASP.NET Identity 还是很给力的,不用白不用 ^_^~ 点击确定之后,会看 ...
- PHP导出XML格式的EXCEL
<?php function Export(){ set_time_limit(0); ob_start(); $biz = new ZaikuBiz(); $biz->setSearch ...
- SWD下载k60
转:JTAG各类接口针脚定义,含义及SWD接线方式 IAR设置如下
- Bad state: Stream has already been listened to.
https://stackoverflow.com/questions/51396769/flutter-bad-state-stream-has-already-been-listened-to T ...