1

Question quite similar to this is answered here and here

And they suggest to use scanBasePackages in the annotation @SpringBootApplication and it works to find all beans required for auto wiring.

Problem Description

However, in one of the service CategoryServiceImpl there is an autowired dao categoryDao which has @PersistenceContext entity manager.

And my spring boot application is unable to find EntityManagerFactory which is the point where my application is breaking.

It is a very straight forward application( I am trying to learn). I have two simple projects.

First one is the rest controller and second one is the JPA project to persist entities.

FIRST PROJECT: REST CONTROLLER PROJECT SETUP

Application.java (entry point)

 @SpringBootApplication(scanBasePackages = { 
              "com.example.resourcecontroller",
              "com.example.service",
              "com.example.dao",
              "javax" })
        public class Application {
    }

pom.xml

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

SECOND PROJECT: JPA SPRING HIBERNATE

pom.xml

   <groupId>com.example</groupId>
    <artifactId>spring-hibernate-jpa-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
<dependency>
    <groupId>com.example</groupId>
    <artifactId>spring-hibernate-jpa-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

Service class

 @Configuration
    @EnableTransactionManagement
    @ComponentScans(value = { @ComponentScan("com.example.dao"),
          @ComponentScan("com.example.service") })

    @Service
    public class CategoryServiceImpl implements CategoryService {

        @Autowired
        private CategoryDao categoryDao;
    }

DAO

  @Repository
    public class CategoryDaoImpl implements CategoryDao {

        @PersistenceContext
        private EntityManager em;
}

**Application Config **

@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.dao"),
      @ComponentScan("com.example.service") })
public class AppConfig {
   @Bean
   public LocalEntityManagerFactoryBean geEntityManagerFactoryBean() {
      LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
      factoryBean.setPersistenceUnitName("LOCAL_PERSISTENCE");
      return factoryBean;
   }

}
Geek
  • 627
  • 1
  • 8
  • 18
  • 1
    Where do you import your AppConfig.java ? (`@Import({... /*list of @Configuration classes*/ .... })`) – Nikolai Shevchenko Jan 28 '18 at 07:12
  • Why do you have an AppConfig class? This is all Spring Boot standard – Simon Martinelli Jan 28 '18 at 11:36
  • @NikolayShevchenko , I think you pointed me to the right direction. However I am getting the issue that persistence.xml not found. Which seems to be a different issue as springboot by default doesnt support persistence.xml. – Geek Jan 28 '18 at 19:02
  • @SimonMartinelli my second project (JPA) was developed without the use of spring boot hence it used AppConfig. Now I am integrating my JPA project with the Springboot managed REST controller. – Geek Jan 28 '18 at 19:02
  • Try adding `@EntityScan("package.entities") @EnableJpaRepositories(basePackages = {"package.repositories"})` onto your spring boot application class – Nikolai Shevchenko Jan 29 '18 at 05:04

0 Answers0