I have a LocalContainerEntityManagerFactoryBean as EntityManager instance.
To quickly drop a full tables\' content, I want to run the follo
@Transactional annotation should not be applied on Dao method but on a service method. Although your code says DatabaseService is a service, but by inserting EntityManger inside a service does not make any sense.
Correct way to implement is to create a Dao like below.
@Repository
public class DatabaseDao {
@PersistenceContext
private EntityManager em;
public void clear() {
em.createNativeQuery("TRUNCATE TABLE MyTable").executeUpdate();
}
}
Then call the dao method from a service method with @Transactional annotation.
@Service
public class DatabaseService {
@Autowired
private DatabaseDao dao;
@Transactional
public void clear() {
dao.clear();
}
}
Also, add @EnableTransactionManagement in your Configuration class