0

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 ?

pheromix
  • 18,213
  • 29
  • 88
  • 158
  • See https://stackoverflow.com/questions/18411803/autowired-entitymanagerfactory-is-null – Ori Marko Mar 04 '19 at 14:18
  • Can you elaborate on the reason why you need to access entity manager? You see, newest version of spring you no longer need it, you can manage persistance thru Repositories, take a look at https://stackoverflow.com/questions/33074547/spring-boot-configure-entitymanager – Cristian Colorado Mar 04 '19 at 14:47
  • @CristianColorado The reason is that I want to manipulate `SessionFactory` ; and every research I made needs working with an instance of an `EntityManagerFactory`. – pheromix Mar 04 '19 at 14:48
  • Can you elaborate on the specific manipulation you ant to make to SessionFactory? Is it just for a specific transaction or is it for your global configuration? – Cristian Colorado Mar 04 '19 at 14:58
  • With `Sessionfactory` I can make a query with the `IN` operator. – pheromix Mar 04 '19 at 15:00
  • If you just want to make queries which make use of the `IN` operator, then have a look at the `CriteriaBuilder` and this related answer: [JPA Criteria builder IN clause query](https://stackoverflow.com/a/42531461/11133168). – FlorianDe Mar 04 '19 at 15:10
  • 1
    Unless it is a complex query you could use spring to define a query using IN like List findByFieldIn(List items); you can take a look at https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation – Cristian Colorado Mar 04 '19 at 15:12
  • @FlorianDe but how to do if there are joins and aggregate functions ? – pheromix Mar 05 '19 at 05:29

1 Answers1

2

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();
    }
}
ManojP
  • 6,113
  • 2
  • 37
  • 49
  • But how do you pass the `EntityManagerFactory` as parameter value ????? – pheromix Mar 04 '19 at 14:47
  • 1
    If your class is annotated with @Component or one of its sub-types and within range of the component scan, then spring will do the dependency injection for you and create singleton instances of the annotated classes. Check [spring boot dependency injection](https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/html/using-boot-spring-beans-and-dependency-injection.html) for more information on that topic. – FlorianDe Mar 04 '19 at 14:52
  • @pheromix As FlorianDe mentioned you don't need to explicitly pass the constructor parameter, It will be taken care by Spring DI. – ManojP Mar 04 '19 at 14:59
  • so the instance variable `emf` should be autowired ! isnt it ? – pheromix Mar 04 '19 at 15:04