Spring JPA - 删除One-toMany关系中的Child

时间:2016-03-05 12:12:52

标签: hibernate jpa spring-data spring-data-jpa

我有以下作者类:

@Entity
public class Author  implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinTable(
        name="author_book",
        joinColumns = @JoinColumn( name="author_id"),
        inverseJoinColumns = @JoinColumn( name="book_id")
    )
    private Set<Book> books= new HashSet<Book>();

    public Author() {
        super();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<Book> getBooks() {
       return books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }
 }

我还有以下图书实体:

@Entity
public class Book implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    public Book() {
        super();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}

接下来,我有以下两个存储库:

public interface BookRepository extends CrudRepository<Book, Long> {
}
public interface AuthorRepository extends CrudRepository<Author, Long>{
}

最后,我有以下测试用例:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class AuthorRepositoryIntegrationTests {

    @Autowired
    AuthorRepository authorRepository;
    @Autowired
    BookRepository bookRepository;

    @Test
    @Transactional
    public void deleteBookFromAuthor() {
        Book book= new Book();
        Author author= new Author();
        author.getBooks().add(book);
        author=this.authorRepository.save(author);

        Iterable<Author> authors = this.authorRepository.findAll();
        author=authors.iterator().next();
        book=author.getBooks().iterator().next();
        this.bookRepository.delete(book);

        authors = this.authorRepository.findAll();
        assertEquals(authors.iterator().next().getBooks().size(),0);
    }
}

我的问题是这个测试用例中的最后一个断言是失败的。如果我向作者添加一本书,它会自动添加到图书库中。但是,如果我从存储库中删除一本书,我该如何从作者中删除它?

2 个答案:

答案 0 :(得分:0)

重做:好的,道歉,我对你的问题看起来不够近。答案虽然很简单,但这次我真的尝试过了。您需要从POJO中的父项中删除子项,并在没有子项的情况下重新保存父项。请参阅下面的注释代码:

List<Book> books = new ArrayList<Book>();

Book book = new Book();
books.add(book);

Author author = new Author();
author.setBooks(books);
authorRepository.save(author);

Author a =authorRepository.findAll().iterator().next();
Book b = a.getBooks().get(0);
bookRepository.delete(b);

// REMOVE FROM PARENT
a.getBooks().remove(0);
authorRepository.save(a);

a = authorRepository.findAll().iterator().next();
System.out.println(a);

编辑:如果您担心一致性,那么只需从作者POJO中删除该书,当您保存作者时,cascade=CascadeType.ALL将为您宣传删除该书。 E.g:

Author a =authorRepository.findAll().iterator().next();
a.getBooks().remove(0);
authorRepository.save(a);

// NO BOOK
a = authorRepository.findAll().iterator().next();
System.out.println(a);

答案 1 :(得分:0)

如果你使用JPA为hibernate orphanRemoval=true

添加一对多关系CascadeType.DELETE_ORPHAN