本文中,我们会将会通过一个Book和Category的关联关系,来讲解如何在JPA中使用。
添加依赖
我们还是使用H2内存数据库来做测试:
org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime
构建Entity
下面我们构建两个Entity:
@Data@Entitypublic class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @ManyToOne private 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 books;}
上面我们定义了两个Entity,Category和Book是一对多的关系。我们通过@ManyToOne和@OneToMany来定义相应的关系。
构建Repository
我们接下来构建相应的Repository:
public interface BookRepository extends CrudRepository { 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 {}
构建初始数据
为了方便测试,我们先构建需要的数据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中删除一条数据:
@Test public void whenDeleteByIdFromRepository_thenDeletingShouldBeSuccessful() { assertThat(bookRepository.count()).isEqualTo(2); bookRepository.deleteById(1L); assertThat(bookRepository.count()).isEqualTo(1); }
再看一下category的删除:
@Test public void whenDeletingCategories_thenBooksShouldAlsoBeDeleted() { categoryRepository.deleteAll(); assertThat(bookRepository.count()).isEqualTo(0); assertThat(categoryRepository.count()).isEqualTo(0); }
再看一下book的删除:
@Test public void whenDeletingBooks_thenCategoriesShouldAlsoBeDeleted() { bookRepository.deleteAll(); assertThat(bookRepository.count()).isEqualTo(0); assertThat(categoryRepository.count()).isEqualTo(1); }
因为我们只在Category中指定了cascade = CascadeType.ALL, 所以删除category的时候可以删除相关联的Book,但是删除Book的时候不会删除相关联的category。
欢迎关注我的公众号:程序那些事,更多精彩等着您!
更多内容请访问:flydean的博客 flydean.com
![0d6e8929fa91f09859abfca592c553d0.png](https://img-blog.csdnimg.cn/img_convert/0d6e8929fa91f09859abfca592c553d0.png)