pojo:

package com.example.zs.springDataJpa;

import org.hibernate.annotations.Proxy;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; @Entity
@Table(name="t_role")
public class UserRole {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id ;
@Column(name="name")
private String name;
@Column(name="name1")
private String name1;
@Column(name="name2")
private String name2;
@OneToMany(mappedBy="userRolehh", fetch = FetchType.EAGER)
private Set<JpaUser> userSet = new HashSet<>();
@ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
//创建t_role_menu表,PRIMARY KEY (`roleid`,`menuid`),
// FOREIGN KEY (`roleid`) REFERENCES `t_role` (`id`)
//FOREIGN KEY (`menuid`) REFERENCES `t_menu` (`menu_id`)
//不改变t_role表,不改变t_menu表

@JoinTable(name="t_role_menu",joinColumns =@JoinColumn(name="roleid") ,inverseJoinColumns =@JoinColumn(name="menuid"))
// private Set<Menu> menuList = new HashSet<>() ; set list都可以
private List<Menu> menuList = new ArrayList<>() ; 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 Set<JpaUser> getUserSet() {
return userSet;
} public void setUserSet(Set<JpaUser> userSet) {
this.userSet = userSet;
} public String getName1() {
return name1;
} public void setName1(String name1) {
this.name1 = name1;
} public String getName2() {
return name2;
} public void setName2(String name2) {
this.name2 = name2;
} public List<Menu> getMenuList() {
return menuList;
}
}
package com.example.zs.springDataJpa;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Proxy; import javax.persistence.*; @Entity
@Table(name="t_user")
public class JpaUser {
// @Column(name="name")
private String name ;
// @Column(name="address")
private String address ;
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
// @GeneratedValue(strategy=GenerationType.TABLE,generator="table_gen")
// @TableGenerator(
// name = "table_gen",
// table="fendo_generator",
// pkColumnName="seq_name", //指定主键的名字
// pkColumnValue="fendos", //指定下次插入主键时使用默认的值
// valueColumnName="seq_id", //该主键当前所生成的值,它的值将会随着每次创建累加
// initialValue = 1, //初始化值
// allocationSize=1 ) //累加值
@Id
private Integer id ;
// @Column(name="age1")
private Integer age ;
// @Column(name="age11")
private Integer age1 ;
// @ManyToOne(cascade = CascadeType.PERSIST)
@ManyToOne
@JoinColumn(name="roleFK")
private UserRole userRolehh; public JpaUser(String name, String address, Integer age, Integer age1 ) {
this.name = name;
this.address = address;
this.age = age;
this.age1 = age1;
// this.id = id;
} public JpaUser() {
} @Override
public String toString() {
return "JpaUser{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", id=" + id +
", age=" + age +
", age1=" + age1 +
'}';
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Integer getAge1() {
return age1;
} public void setAge1(Integer age1) {
this.age1 = age1;
} public UserRole getUserRolehh() {
return userRolehh;
} public void setUserRole(UserRole userRolehh) {
this.userRolehh = userRolehh;
}
}
package com.example.zs.springDataJpa;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; @Entity
@Table(name ="t_menu")
public class Menu {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer menuId ;
private String menuName ;
private String menuUrl;
private Integer fatherId ;
//menu跟role是多对多关系
@ManyToMany(mappedBy ="menuList" )
private Set<UserRole> roleSet = new HashSet<>();
// private List<UserRole> roleList = new ArrayList<>(); public void setMenuId(Integer menuId) {
this.menuId = menuId;
} public void setMenuName(String menuName) {
this.menuName = menuName;
} public void setMenuUrl(String menuUrl) {
this.menuUrl = menuUrl;
} public void setFatherId(Integer fatherId) {
this.fatherId = fatherId;
} public void setRoleSet(Set<UserRole> roleSet) {
this.roleSet = roleSet;
} public Set<UserRole> getRoleSet() {
return roleSet;
} public Integer getMenuId() {
return menuId;
}
}
package com.example.zs.springDataJpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface UserRoleSpecificationExcutor extends JpaRepository<UserRole,Integer> , JpaSpecificationExecutor<UserRole> {
}

保存示例:

  @Test
public void save () {
UserRole userRole = new UserRole();
userRole.setName("角色");
Menu menu = new Menu();
Menu menu1 = new Menu();
userRole.getMenuList().add(menu);
userRole.getMenuList().add(menu1);
menu.setMenuName("顶级菜单");
menu.setMenuUrl("菜单0");
menu.setFatherId();
menu1.setMenuName("菜单1");
menu1.setMenuUrl("菜单1");
menu1.setFatherId(); userRoleSpecificationExcutor.save(userRole); // Optional<UserRole> op = userRoleSpecificationExcutor.findById(1);
// System.out.println(op);
}

查询示例:

@Test
public void test2 () { // Optional<JpaUser> a= userSpecificationExcutor.findById(13);
Optional<UserRole> ab = userRoleSpecificationExcutor.findById();
System.out.println("---"); // System.out.println(optionalT.get());
// userRespotory.findByIdxUseHQL("hehe",1);
// for(JpaUser user : list){
// }
}

spring boot - 整合jpa多对对关系保存和查询示例的更多相关文章

  1. spring boot 系列之四:spring boot 整合JPA

    上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...

  2. Spring Boot整合JPA、Redis和Swagger2

    好久没有总结了,最近也一直在学习.今天就把spring boot与其它技术的整合做个小总结,主要是jpa.redis和swagger2.公司里有用到这些,整合起来也很简单. 首先,新建一个Spring ...

  3. Spring Boot 整合 JPA 使用多个数据源

    介绍 JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的标准规范,Hibernate 是持久化规范的技术实现,而 Spring Data JPA 是在 ...

  4. Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源

    本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...

  5. Spring Boot2 系列教程(二十四)Spring Boot 整合 Jpa

    Spring Boot 中的数据持久化方案前面给大伙介绍了两种了,一个是 JdbcTemplate,还有一个 MyBatis,JdbcTemplate 配置简单,使用也简单,但是功能也非常有限,MyB ...

  6. Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany

    问题描述 在利用Spring boot data JPA进行表设计的时候,表对象之间经常存在各种映射关系,如何正确将理解的映射关系转化为代码中的映射关系是关键之处. 解决办法 概念理解 举例:在公司的 ...

  7. spring boot 整合JPA多数据源

    上个文章介绍了spring boot在使用Mybatis持久化技术的时候如何使用多数据源,今天再补充一个使用spring data jpa实现多数据源的使用情况,JPA是一套数据库持久化规范,或者称之 ...

  8. Spring Boot从入门到精通之:二、Spring Boot整合JPA

    springboot-jpa 开发工具 系统: windows10 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_ ...

  9. spring boot - 整合jpa

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

随机推荐

  1. PHP使用urlencode对中文编码时空格、加号的问题

    使用urlencode这个函数进行格式化,urlencode函数会把空格编码为为:+ 当然,前端在接收时可以解码后进行替换 + 为空格的方式处理. 但是这样就多做了一步,很麻烦,有的时候我们的数据接口 ...

  2. Yii框架学习资源盘点

    盘点一些Yii框架的常用学习资源. 1.Yii中文论坛 https://www.yiichina.com/ 2.Yii中文网 http://www.yii-china.com/ 3.魏曦教你学Yii2 ...

  3. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第五集之补充-使用桥接模式实现虚拟机作为服务器,让同网段的其他主机远程连接】

    参考:https://blog.csdn.net/qicheng777/article/details/73438045 https://www.cnblogs.com/hld123/p/650550 ...

  4. JWT 超详细分析

    请参考以下链接 https://learnku.com/articles/17883

  5. codeforces contest1082

    C 维护前缀和 题意 每一个id给一个权值序列,从每个id选出数量相同的权值,对他们进行求和,使得他们的和最大 题解 注意负数对结果没有贡献,直接跳过. 当时写的比较挫,连排序都写错了!cf的编译器比 ...

  6. nginx配置ssl验证

    在上一篇博客中,我们只是通过nginx搭建了反向代理服务,由于需要在小程序中使用https服务,所以需要申请安全证书. 1.在所购买的域名商那申请免费的ssl证书,我买的是阿里的,所以直接在阿里上申请 ...

  7. python可视化库 Matplotlib 00 画制简单图像

    1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...

  8. CentOS7上安装Snipe-IT4.6.3详细过程及注意事项

    笔者采用的是CentOS7,先对系统进行Update,然后安装军哥的LNMPA,详情请参考lnmp.org 注意:安装LNMPA前需要修改lnmp.conf中这一行为下面,也就是要安装PHP的File ...

  9. #工具 Intellij IDEA中自定义的Maven Archetype管理

    背景,手贱在输入自定义的 archetype时后面多输入了一个空格 解决:自定义的Archetype 会保存在Windows下面的文件中 C:\Users\<user>\.IntelliJ ...

  10. jquery固定表头和列头

    1.对网上的开源方法稍作了些修改 <script type="text/javascript">// <![CDATA[ function FixTable(Ta ...