Spring Boot JPA中关联表的使用
  TEZNKK3IfmPf 2023年11月14日 18 0

文章目录

    • 添加依赖
    • 构建Entity
    • 构建Repository
    • 构建初始数据
    • 测试


Spring Boot JPA中关联表的使用

本文中,我们会将会通过一个Book和Category的关联关系,来讲解如何在JPA中使用。

我们还是使用H2内存数据库来做测试:

        <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>

构建Entity

下面我们构建两个Entity:

@Data@Entitypublic class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String title;@ManyToOneprivate Category category;}

 

@Data@Entitypublic class Category {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)private List<Book> books;}

 

上面我们定义了两个Entity,Category和Book是一对多的关系。我们通过@ManyToOne和@OneToMany来定义相应的关系。

构建Repository

我们接下来构建相应的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> {}

 

构建初始数据

为了方便测试,我们先构建需要的数据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');

 

测试

我们看一下怎么从Book中删除一条数据:

    @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);}

 

因为我们只在Category中指定了cascade = CascadeType.ALL, 所以删除category的时候可以删除相关联的Book,但是删除Book的时候不会删除相关联的category。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
TEZNKK3IfmPf