Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate table in Play Framework/JPA/hibernate?

In mysql, DELETE FROM table is much more expensive than TRUNCATE TABLE table

I believe that JPA's Entity.deleteAll() runs delete. What's the correct way to truncate instead?

like image 681
ripper234 Avatar asked Jan 16 '23 18:01

ripper234


1 Answers

TRUNCATE TABLE table is not standard-SQL, so not generally supported. To do it, execute the query directly:

em().createNativeQuery("truncate table MyTable").executeUpdate();

While in a Play Model class (where you have access to the EntityManager with em().

like image 173
Samuel Avatar answered Jan 18 '23 09:01

Samuel