spring boot - 整合jpa多对对关系保存和查询示例
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多对对关系保存和查询示例的更多相关文章
- spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...
- Spring Boot整合JPA、Redis和Swagger2
好久没有总结了,最近也一直在学习.今天就把spring boot与其它技术的整合做个小总结,主要是jpa.redis和swagger2.公司里有用到这些,整合起来也很简单. 首先,新建一个Spring ...
- Spring Boot 整合 JPA 使用多个数据源
介绍 JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的标准规范,Hibernate 是持久化规范的技术实现,而 Spring Data JPA 是在 ...
- Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...
- Spring Boot2 系列教程(二十四)Spring Boot 整合 Jpa
Spring Boot 中的数据持久化方案前面给大伙介绍了两种了,一个是 JdbcTemplate,还有一个 MyBatis,JdbcTemplate 配置简单,使用也简单,但是功能也非常有限,MyB ...
- Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany
问题描述 在利用Spring boot data JPA进行表设计的时候,表对象之间经常存在各种映射关系,如何正确将理解的映射关系转化为代码中的映射关系是关键之处. 解决办法 概念理解 举例:在公司的 ...
- spring boot 整合JPA多数据源
上个文章介绍了spring boot在使用Mybatis持久化技术的时候如何使用多数据源,今天再补充一个使用spring data jpa实现多数据源的使用情况,JPA是一套数据库持久化规范,或者称之 ...
- Spring Boot从入门到精通之:二、Spring Boot整合JPA
springboot-jpa 开发工具 系统: windows10 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_ ...
- spring boot - 整合jpa
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
随机推荐
- 浅析布隆过滤器及实现demo
布隆过滤器 布隆过滤器(Bloom Filter)是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时,能很好地取舍空间使用率与误报比例. ...
- [原创]windows 部署SS server 出现的错误.
安装过程: .Download and install Python MSI installer in 64bit Windows. .During installation you should i ...
- 有关UnrealEngine材质编辑器中的Custom节点的一些小贴士
PS:本文写于2017.2.1日,使用版本为4.13.第二次更新时间为2017.3.15增加了四.一些材质编辑器中的奇怪的技巧: 一.前言在Unreal中材质编辑器提供了Custom节点,作为HLSL ...
- ubuntu的安装及ubuntu中安装mysql和tomcat
一.安装ubuntu 1.创建虚拟机 2.向导选择自定义 3.然后下一步再下一步,直到这里,稍后再安装系统 4.然后选择linux,注意这里下面的下拉选择Ubuntu64,因为我们下载的是64位的,如 ...
- [CF1093E]Intersection of Permutations
[CF1093E]Intersection of Permutations 题目大意: 给定两个长度为\(n(n\le2\times10^5)\)的排列\(A,B\).\(m(m\le2\times1 ...
- 【转】Zookeeper 安装和配置
转自:http://coolxing.iteye.com/blog/1871009 Zookeeper的安装和配置十分简单, 既可以配置成单机模式, 也可以配置成集群模式. 下面将分别进行介绍. 单机 ...
- web应用、HTTP协议及web框架简介
1. web应用 1.1 web应用程序 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件 B/S模式(浏览器/服 ...
- 登录RabbitMQ的方法
一:(运行RabbitMQ之前需要先打开docker 容器)打开相应的路径,在windows Powershell 管理员下打开 输入:docker-compose -f .\docker-compo ...
- php代码进行跨域请求处理
以下的函数作为每个请求的前置操作 (thinkphp框架) public function appInit(&$params) { header('Access-Control-Allow-O ...
- [LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...