spring data jpa关联查询(一对一、一对多、多对多)
在实际过往的项目中,常用的查询操作有:1、单表查询,2、一对一查询(主表和详情表)3、一对多查询(一张主表,多张子表)4、多对多查询(如权限控制,用户、角色多对多)。做个总结,所以废话不多说。
使用idea构建springboot项目,引入依赖如下:
dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
使用h2数据库做测试用,application.yml配置如下:
spring:
jpa:
generate-ddl: true
hibernate:
ddl-auto: update
properties:
hibenate:
format_sql: false
show-sql: true
首先,一对一有好几种,这里举例的是常用的一对一双向外键关联(改造成单向很简单,在对应的实体类去掉要关联其它实体的属性即可),并且配置了级联删除和添加,相关类如下:
package io.powerx; import lombok.*; import javax.persistence.*; /**
* Created by Administrator on 2018/8/15.
*/
@Getter
@Setter
@Entity
public class Book {
@Id
@GeneratedValue
private Integer id; private String name; @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.REMOVE})
@JoinColumn(name="detailId",referencedColumnName = "id")
private BookDetail bookDetail; public Book(){
super();
}
public Book(String name){
super();
this.name =name;
} public Book(String name, BookDetail bookDetail) {
super();
this.name = name;
this.bookDetail = bookDetail;
}
@Override
public String toString() {
if (null == bookDetail) {
return String.format("Book [id=%s, name=%s, number of pages=%s]", id, name, "<EMPTY>");
} return String.format("Book [id=%s, name=%s, number of pages=%s]", id, name, bookDetail.getNumberOfPages());
}
}
package io.powerx; import lombok.Getter;
import lombok.Setter; import javax.persistence.*; @Getter
@Setter
@Entity(name = "BOOK_DETAIL")
public class BookDetail { @Id
@GeneratedValue
private Integer id; @Column(name = "NUMBER_OF_PAGES")
private Integer numberOfPages; @OneToOne(mappedBy = "bookDetail")
private Book book; public BookDetail() {
super();
} public BookDetail(Integer numberOfPages) {
super();
this.numberOfPages = numberOfPages;
} @Override
public String toString() {
if (null == book) {
return String.format("Book [id=%s, name=%s, number of pages=%s]", id, "<EMPTY>");
} return String.format("Book [id=%s, name=%s, number of pages=%s]", id,book.getId(),book.getName());
}
}
package io.powerx; import org.springframework.data.jpa.repository.JpaRepository; /**
* Created by Administrator on 2018/8/15.
*/
public interface BookRepository extends JpaRepository<Book,Integer> {
Book findByName(String name);
}
package io.powerx; import org.springframework.data.jpa.repository.JpaRepository; /**
* Created by Administrator on 2018/8/15.
*/
public interface BookDetailRepository extends JpaRepository<BookDetail, Integer>{ BookDetail findByNumberOfPages(Integer numberOfPages);
}
package io.powerx; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.Arrays; import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class)
@SpringBootTest
public class OnetooneApplicationTests { @Autowired
private BookRepository bookRepository; @Autowired
private BookDetailRepository bookDetailRepository; @Before
public void init() {
Book bookA = new Book("Spring in Action", new BookDetail(208));
Book bookB = new Book("Spring Data in Action", new BookDetail(235));
Book bookC = new Book("Spring Boot in Action");
bookRepository.saveAll(Arrays.asList(bookA, bookB, bookC));
} @After
public void clear() {
bookRepository.deleteAll();
} @Test
public void find() {
Book book = bookRepository.findByName("Spring in Action");
System.err.println(book.toString());
} @Test
public void save() {
Book book = new Book("springboot");
BookDetail bookDetail = new BookDetail(124);
book.setBookDetail(bookDetail);
bookRepository.save(book);
} @Test
public void delete() { bookRepository.deleteById(31);
}
@Test
public void findbook(){
BookDetail bd = bookDetailRepository.findByNumberOfPages(235);
System.err.println(bd.toString()); }
}
一对多双向,相关类如下:
package io.powerx; import lombok.Data;
import lombok.Getter;
import lombok.Setter; import javax.persistence.*; @Getter
@Setter
@Entity
public class Book {
@Id
@GeneratedValue
private Integer id; private String name; @ManyToOne
@JoinColumn(name="publishId")
private Publisher publisher; @Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", publisher=" + publisher.getName() +
'}';
} public Book(String name) {
this.name = name;
} public Book() {
}
}
package io.powerx; import lombok.Data;
import lombok.Getter;
import lombok.Setter; import javax.persistence.*;
import java.util.HashSet;
import java.util.Set; /**
* Created by Administrator on 2018/8/16.
*/
@Getter
@Setter
@Entity
public class Publisher {
@Id
@GeneratedValue
private Integer id; private String name; @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="publishId",referencedColumnName = "id")
private Set<Book> books; public Publisher() {
super();
} public Publisher(String name) {
super();
this.name = name;
} @Override
public String toString() {
return "Publisher{" +
"id=" + id +
", name='" + name + '\'' +
", books=" + books.size() +
'}';
} }
package io.powerx; import org.springframework.data.jpa.repository.JpaRepository; /**
* Created by Administrator on 2018/8/16.
*/
public interface BookRepository extends JpaRepository<Book,Integer>{ Book findByName(String name); }
package io.powerx; import org.springframework.data.jpa.repository.JpaRepository; /**
* Created by Administrator on 2018/8/16.
*/
public interface PublisherRepository extends JpaRepository<Publisher,Integer> { Publisher findByName(String name);
}
package io.powerx; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.HashSet;
import java.util.Set; @RunWith(SpringRunner.class)
@SpringBootTest
public class OnetomanyApplicationTests { @Autowired
private PublisherRepository publisherRepository; @Autowired
private BookRepository bookRepository; @Before
public void init() { Book book1 = new Book("spring");
Book book2 = new Book("mvc");
Book book3 = new Book("mybatis");
Publisher publisher = new Publisher("zhonghua");
Set<Book> set = new HashSet<Book>();
set.add(book1);
set.add(book2);
set.add(book3);
publisher.setBooks(set);
publisherRepository.save(publisher); } @After
public void clear() {
publisherRepository.deleteAll();
} @Test
public void find() {
Publisher publisher = publisherRepository.findByName("zhonghua");
System.out.println(publisher);
} @Test
public void find2() {
Book book = bookRepository.findByName("mvc");
System.out.println(book);
}
}
多对多双向,相关代码如下:
package io.powerx; import lombok.Getter;
import lombok.Setter;
import lombok.ToString; import javax.persistence.*;
import java.util.Set; @Getter
@Setter
@Entity
public class Author { @Id
@GeneratedValue
private Integer id; private String name; @ManyToMany(mappedBy = "authors",fetch = FetchType.EAGER)
private Set<Book> books; public Author() {
super();
} public Author(String name) {
super();
this.name = name;
} @Override
public String toString() {
return "Author{" +
"id=" + id +
", name='" + name + '\'' +
", books=" + books.size() +
'}';
}
}
package io.powerx; import lombok.Getter;
import lombok.Setter;
import lombok.ToString; import javax.persistence.*;
import java.util.HashSet;
import java.util.Set; @Getter
@Setter
@Entity
public class Book { @Id
@GeneratedValue
private Integer id; private String name; @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "BOOK_AUTHOR", joinColumns = {
@JoinColumn(name = "BOOK_ID", referencedColumnName = "ID")}, inverseJoinColumns = {
@JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID")})
private Set<Author> authors; public Book() {
super();
} public Book(String name) {
super();
this.name = name;
this.authors = new HashSet<>();
} public Book(String name, Set<Author> authors) {
super();
this.name = name;
this.authors = authors;
} @Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", authors=" + authors.size() +
'}';
}
}
package io.powerx; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface AuthorRepository extends JpaRepository<Author, Integer> { Author findByName(String name); List<Author> findByNameContaining(String name); }
package io.powerx; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface BookRepository extends JpaRepository<Book, Integer> { Book findByName(String name); List<Book> findByNameContaining(String name); }
在调试过程中,注意实体类的tostring方法的重写,避免相互引用;此外如果超过两张表的关联查询,建议使用自定义sql,建立相应的pojo来接收查询结果。
spring data jpa关联查询(一对一、一对多、多对多)的更多相关文章
- spring data jpa 分页查询
https://www.cnblogs.com/hdwang/p/7843405.html spring data jpa 分页查询 法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特 ...
- spring data JPA entityManager查询 并将查询到的值转为实体对象
spring data JPA entityManager查询 并将查询到的值转为实体对象 . https://blog.csdn.net/qq_34791233/article/details/81 ...
- 【Spring Data 系列学习】Spring Data JPA 基础查询
[Spring Data 系列学习]Spring Data JPA 基础查询 前面的章节简单讲解了 了解 Spring Data JPA . Jpa 和 Hibernate,本章节开始通过案例上手 S ...
- Spring Boot 入门系列(二十七)使用Spring Data JPA 自定义查询如此简单,完全不需要写SQL!
前面讲了Spring Boot 整合Spring Boot JPA,实现JPA 的增.删.改.查的功能.JPA使用非常简单,只需继承JpaRepository ,无需任何数据访问层和sql语句即可实现 ...
- springboot集成Spring Data JPA数据查询
1.JPA介绍 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.它的出现主要是为 ...
- Spring Data JPA 简单查询--接口方法
一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...
- Spring Data JPA 实例查询
一.相关接口方法 在继承JpaRepository接口后,自动拥有了按"实例"进行查询的诸多方法.这些方法主要在两个接口中定义,一是QueryByExampleExecut ...
- springboot整合spring data jpa 动态查询
Spring Data JPA虽然大大的简化了持久层的开发,但是在实际开发中,很多地方都需要高级动态查询,在实现动态查询时我们需要用到Criteria API,主要是以下三个: 1.Criteria ...
- Spring Data JPA 条件查询的关键字
Spring Data JPA 为此提供了一些表达条件查询的关键字,大致如下: And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(Stri ...
随机推荐
- C#函数式程序设计之泛型(上)
在面向对象语言中,我们可以编写一个元素为某个专用类型(可能需要为此创建一个ListElement)的List类,或者使用一个非常通用.允许添加任何类型元素的基类(在.NET中,首先想到的是System ...
- 记一次阿里云linux病毒清理过程
1.起因 因为这台服务器是我们公司内部开发服务器,几乎每个人都有root密码.在两天前突然有同事反馈说偶尔会有ssh连不上,git代码无法提交的问题,刚开始也没有在意,以为是阿里云服务器网络波动的 ...
- Fiddler手机https抓包
Fiddler手机抓包:https://blog.csdn.net/wangjun5159/article/details/52202059 fiddler 使用说明:https://www.cnbl ...
- SQL Server分页模板
SQL Server分页模板 WITH T AS ( SELECT ROW_NUMBER() OVER(ORDER BY AlbumId ) AS row_number, * FROM (SELECT ...
- SAH Benchmarks Of Natural History Museum Scene
method ...
- HttpRunnerManager接口自动化测试框架在win环境下搭建教程
近几日一直在研究如何把接口自动化做的顺畅,目前用的是轻量级jmeter+ant+Jenkins自动化测试框架,目前测试界的主流是python语言,所以一直想用搭建一个基于python的HttpRunn ...
- “全栈2019”Java第三十一章:二维数组和多维数组详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- AtCoder Grand Contest 031题解
题面 传送门 题解 比赛的之后做完\(AB\)就开始发呆了--简直菜的一笔啊-- \(A - Colorful\ Subsequence\) 如果第\(i\)个字母选,那么它前面任意一个别的字母的选择 ...
- PHP如何将多维数组中的数据批量插入数据库?
PHP将多维数组中的数据批量插入到数据库中,顾名思义,需要用循环来插入. 1.循环insert into 语句,逐渐查询 <?php /* www.qSyz.net */ @mysql_conn ...
- linux awk 文本分析实例 [视频]
实例: 点我打开哔哩哔哩 实战: 点我打开哔哩哔哩 以下是一个分享: ➜ log Time Now 16:23:35 > awk '/Installed/' yum.log* | awk -F ...