6

I am using Spring Boot 2.0.0.RC1 (It include Spring Framework 5.0.3.RELEASE), Hibernate 5.2.12.Final, JPA 2.1 API 1.0.0.Final .

I have a class

package com.example;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManagerFactory;

@Configuration
public class BeanConfig {

    @Autowired
    EntityManagerFactory emf;

    @Bean
    public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
        return emf.unwrap(SessionFactory.class);
    }

}

Then error

Error
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method sessionFactory in com.example.BeanConfig required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.


Action:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.


Process finished with exit code 1

How to fix this?

Vy Do
  • 46,709
  • 59
  • 215
  • 313

3 Answers3

7

If you include this:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

You won't have to autowire the Entity Manager or provide a Session Factory bean.

You would only need to provide JpaRepository interfaces like:

public interface ActorDao extends JpaRepository<Actor, Integer> {
}

where Actor is a JPA entity class and Integer is the ID / primary key and inject ActorDao in a service impl class.

ootero
  • 3,235
  • 2
  • 16
  • 22
  • I tend to use full feature Hibernate. Before the question, I created JPA implement of Hibernate with Spring Boot 2.0.0.RC1 success. – Vy Do Feb 21 '18 at 13:22
  • I didn't get your comment, but again, if you are including `spring-boot-starter-data-jpa` in your `pom`, there is no need for you to explicitly provide `EntityManagerFactory` and / or `SessionFactory` beans. `Spring Boot` provides them for you based on dependencies found in the classpath and configuration properties. BTW You are missing `@EnableJpaRepositories` in your `Application.java` – ootero Feb 21 '18 at 19:41
2

In BeanConfig, you should inject the JPA EntityManager via @PersistenceUnit, not @Autowired.

And remove the getSessionFactory since the Hibernate SessionFactory is already created internally and you can always unwrap the EntityManagerFactory.

Like this:

@Configuration
public class BeanConfig {

    @PersistenceUnit
    EntityManagerFactory emf;

}
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Thank you very much! I catch error: https://gist.github.com/donhuvy/d60b1569144162ea69833d881ef7137f This is [screen shot](https://user-images.githubusercontent.com/1328316/36632373-a4091072-19b8-11e8-9103-eeb28bee9ff0.png) – Vy Do Feb 24 '18 at 16:15
  • Nope, still wrong. I added an example for how the `BeanConfig` should look like. – Vlad Mihalcea Feb 24 '18 at 16:23
  • Thank you very much! I have problem at DAOImplement class . When use `EntityManagerFactory` how to query? https://github.com/donhuvy/spring_boot_hibernate/blob/master/src/main/java/com/example/dao/impl/UserDaoImpl.java#L22 – Vy Do Feb 24 '18 at 16:32
  • 1
    I guess you didn't read the manual, right? You need to inject a JPA EntityManager using @PersistenceContext. – Vlad Mihalcea Feb 24 '18 at 16:33
  • Yes, I am sorry, I don't read user manual carefully. Please point out for me! – Vy Do Feb 24 '18 at 16:35
  • 1
    You should read the entire Spring, Spring Data and Spring Boot manuals. For a short reference to using a JPA EntityManager, read this https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#dao-annotations – Vlad Mihalcea Feb 24 '18 at 16:42
0

The specific error you are having is caused by the @Qualifier annotation; Spring is looking for a Bean with the specific name you mentioned, instead of looking for any Bean of type EntityManagerFactory. Just remove the annotation.

However, once you fix that, and because you are also injecting the Bean in the method that constructs SessionFactory, Spring Boot will generate another error related to cyclic dependencies. To avoid that, just remove the parameter altogether from sessionFactory method, since you already injected EntityManagerFactory in your Config class.

This code will work :

@Bean
public SessionFactory sessionFactory() {
        return emf.unwrap(SessionFactory.class);
}
HL'REB
  • 838
  • 11
  • 17
  • I tested it and it works, with your POM file. What error do you have? – HL'REB Feb 21 '18 at 14:09
  • After apply your code, this is console log https://gist.github.com/donhuvy/c0c5ef0abbf9130b2bce58b5e2a4bdd7 – Vy Do Feb 21 '18 at 15:33
  • Error happen maybe by JDK 9, but I am not sure because I haven't chance to test at this time. – Vy Do Feb 27 '18 at 17:18