如何删除所有JPA实体?

如何删除所有JPA实体?

问题描述:

在我的测试代码中,每种方法都需要有一个空白/空数据库.有没有可以实现此目的的代码,可以调用测试的@Before?

In my testing code I need to have a blank/empty database at each method. Is there code that would achieve that, to call in the @Before of the test?

实际上,您始终可以使用JPQL,

Actually you always can use JPQL,

em
   .createQuery("DELETE FROM MyEntity m")
   .executeUpdate()
;

但是请注意,没有授予实体缓存也将被清除的授权.但是出于单元测试的目的,这似乎是一个很好的解决方案.

But note, there is no grants that entity cache would be cleaned also. But for unit-test purposes it is look like good solution.