I have one-directional related entities:
@Entity
public class Book {
private String isbn;
}
@Entity
private class Recommentation {
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "book_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Book book;
}
And the following test:
@RunWith(SpringRunner.class)
@DataJpaTest
public class BookRepositoryTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private BookRepository bookRepository;
@Test
public void delete() {
// given
String isbn = "isbn-1";
Book book = new Book();
book.setIsbn(isbn);
testEntityManager.persist(book);
Recommendation recommendation = new Recommendation();
recommendation.setBook(book);
testEntityManager.persist(recommendation);
// when
bookRepository.deleteBookByIsbn(book.getIsbn());
// then
assertThat(testEntityManager.find(Book.class, book.getId())).isNull();
assertThat(testEntityManager.find(Recommendation.class, recommendation.getId())).isNull();
}
}
@OnDelete(action = OnDeleteAction.CASCADE)
works perfectly well when this code is invoked not from test but in the test I get exception that recommendation isn't deleted by book.
Also I try to get any info from sql logged for hibernate queries and I can't see any delete
statements for this test.
I don't want to use bidirectional linking for entities and just try to understand how to solve this concrete issue or debug it somehow.