javascript
Spring Boot JPA中关联表的使用
文章目錄
- 添加依賴
- 構(gòu)建Entity
- 構(gòu)建Repository
- 構(gòu)建初始數(shù)據(jù)
- 測(cè)試
Spring Boot JPA中關(guān)聯(lián)表的使用
本文中,我們會(huì)將會(huì)通過一個(gè)Book和Category的關(guān)聯(lián)關(guān)系,來講解如何在JPA中使用。
添加依賴
我們還是使用H2內(nèi)存數(shù)據(jù)庫來做測(cè)試:
<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>構(gòu)建Entity
下面我們構(gòu)建兩個(gè)Entity:
@Data @Entity public class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String title;@ManyToOneprivate Category category; } @Data @Entity public class Category {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)private List<Book> books; }上面我們定義了兩個(gè)Entity,Category和Book是一對(duì)多的關(guān)系。我們通過@ManyToOne和@OneToMany來定義相應(yīng)的關(guān)系。
構(gòu)建Repository
我們接下來構(gòu)建相應(yīng)的Repository:
public interface BookRepository extends CrudRepository<Book, Long> {long deleteByTitle(String title);@Modifying@Query("delete from Book b where b.title=:title")void deleteBooks(@Param("title") String title); } public interface CategoryRepository extends CrudRepository<Category, Long> {}構(gòu)建初始數(shù)據(jù)
為了方便測(cè)試,我們先構(gòu)建需要的數(shù)據(jù)schema.sql和data.sql:
CREATE TABLE book (id BIGINT NOT NULL AUTO_INCREMENT,title VARCHAR(128) NOT NULL,category_id BIGINT,PRIMARY KEY (id) );CREATE TABLE category (id BIGINT NOT NULL AUTO_INCREMENT,name VARCHAR(128) NOT NULL,PRIMARY KEY (id) ); insert into book(id,title,category_id) values(1,'The Hobbit',1); insert into book(id,title,category_id) values(2,'The Rabbit',1);insert into category(id,name) values(1,'category');測(cè)試
我們看一下怎么從Book中刪除一條數(shù)據(jù):
@Testpublic void whenDeleteByIdFromRepository_thenDeletingShouldBeSuccessful() {assertThat(bookRepository.count()).isEqualTo(2);bookRepository.deleteById(1L);assertThat(bookRepository.count()).isEqualTo(1);}再看一下category的刪除:
@Testpublic void whenDeletingCategories_thenBooksShouldAlsoBeDeleted() {categoryRepository.deleteAll();assertThat(bookRepository.count()).isEqualTo(0);assertThat(categoryRepository.count()).isEqualTo(0);}再看一下book的刪除:
@Testpublic void whenDeletingBooks_thenCategoriesShouldAlsoBeDeleted() {bookRepository.deleteAll();assertThat(bookRepository.count()).isEqualTo(0);assertThat(categoryRepository.count()).isEqualTo(1);}因?yàn)槲覀冎辉贑ategory中指定了cascade = CascadeType.ALL, 所以刪除category的時(shí)候可以刪除相關(guān)聯(lián)的Book,但是刪除Book的時(shí)候不會(huì)刪除相關(guān)聯(lián)的category。
本文的例子可以參考https://github.com/ddean2009/learn-springboot2/tree/master/springboot-jpa-relation
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
- java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程
更多教程請(qǐng)參考 flydean的博客
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的Spring Boot JPA中关联表的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot JPA的查询语句
- 下一篇: gradle idea java ssm