I need to clear the tables of my database in a Quarkus application. I am able to achieve this for Entities extending either PanacheEntity
or PanacheEntityBase
by calling entity.deleteAll()
. How can I clear table for embeddable types?
So I was able to fix the issue myself and here's what I did:
Course
and an embeddable CourseDescription
. CourseDescription
is embedded in Course
Example:@Embedded
@ElementCollection
public List<CourseDescription> courseDescriptions;
My Test Class:
@PersistenceContext
EntityManager entityManager;
...
@AfterEach
@Transactional
public void tearDown(){
Query query = entityManager.createNativeQuery("DELETE FROM course_description")
.setHint(COMMENT, "Custom cleanup for embeddable type CourseDescription");
query.executeUpdate();
Course.deleteAll();
}
Update: I could have simply set CourseDescription to null and let JPA take care of the cleanup.
@AfterEach
@Transactional
public void tearDown(){
Course.findAll().stream()
.forEach(course -> {
Course c = (Course) course;
c.courseDescription = null;
});
}