Reputation: 19287
In this tutorial it is said that
Spring Boot configures Hibernate as the default JPA provider, so it’s no longer necessary to define the entityManagerFactory bean unless we want to customize it.
, so how to autowire it inside a class ?
Upvotes: 0
Views: 491
Reputation: 6248
I have used it one of my projects. Please check the below code for your reference.
@Component
public class XXXServiceImpl {
private EntityManagerFactory emf;
@Autowired
private DataSource dataSource;
private final String DropQuery = "DROP table "+Schema_Name + ".";
@Autowired
public XXXServiceImpl(EntityManagerFactory emf) {
Assert.notNull(emf, "EntityManagerFactory must not be null");
this.emf = emf;
}
public void dropAllChildTables(String tableNamePrefix) {
EntityManager entityManager = emf.createEntityManager();
entityManager.getTransaction().begin();
List<?> tables = entityManager.createNativeQuery(ListTableQry).setParameter("namePrefix", tableNamePrefix).getResultList();
tables.forEach(tname -> {
String query = DropQuery + "\"" + tname + "\"";
entityManager.createNativeQuery(query).executeUpdate();
});
entityManager.getTransaction().commit();
entityManager.close();
}
}
Upvotes: 2