4

I have simple Spring 4 WebMVC app (Java-config), and I want to add JPA. But when I try to run app (as deloyed on Tomcat) I get: What can be a source of error?

Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.demo.webtemplate.db.repository.CustSysRepository org.demo.webtemplate.controllers.IndexController.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'custSysRepository': Cannot create inner bean '(inner bean)#f4da8a0' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#f4da8a0': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

Initializer:

package org.demo.webtemplate;

...

public class SpringWebMvcInitializer implements WebApplicationInitializer  {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);
        ctx.setServletContext(servletContext);
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

}

Config:

package org.demo.webtemplate;

...

@Configuration
@EnableWebMvc
@ComponentScan("pl.bzwbk.webtemplate")
@EnableJpaRepositories("pl.bzwbk.webtemplate.db.repository")
public class Config {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

Controller:

package org.demo.webtemplate.controllers;

...

@Controller
public class IndexController {

    @Autowired
    CustSysRepository repository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        List<CustSys> clients = repository.findByFullName("SOME NAME");

        return "index"+clients .size();
    }

}

Repository:

package org.demo.webtemplate.db.repository;

...

public interface CustSysRepository extends JpaRepository<CustSys, Long> {

    List<CustSys> findByFullName(String fullName);
}

Entity:

package org.demo.webtemplate.db.entity;

...

@Entity
@Table(name = "CUST_SYS")
public class CustSys implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    @Size(max = 255)
    @Column(name = "FULL_NAME")
    private String fullName;
    ...
    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    ...
}

application.properties:

jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
jdbc.url=jdbc:hsqldb:mem:testdb
jdbc.user=sa
jdbc.pass=
Son
  • 960
  • 2
  • 14
  • 25
  • No, that's not the same. In case in that link problem was because he change name of entityManagerFactory to emf, and he use XML config, not Java-Config. – Son Feb 16 '15 at 15:19
  • where have you defined entityManagerFactory? – Pranalee Feb 19 '15 at 10:18

2 Answers2

13

You're missing completly DB configuration in your Config class.

Try this for example:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
    dataSource.setUrl("jdbc:hsqldb:mem:testdb");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    return dataSource;
}

@Bean
public EntityManager entityManager() {
    return entityManagerFactory().getObject().createEntityManager();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan("package.where.your.entites.like.CustSys.are.stored");
    return em;
}
rikica
  • 351
  • 2
  • 11
  • 4
    It's in application.properties. – Son Feb 19 '15 at 10:26
  • Only DB properties are in application.properties file. You need to define Spring beans as I suggested. Just look at your exception: Spring cannot find entity manager factory bean; now look at my answer: I defined data source, entity manager and entity manager factory beans... – rikica Feb 24 '15 at 06:34
  • Hi @rikica , Thanks for your answer, it helped me to solve this error. But now I get the following error. "No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either". Can you please help? – Bharath Feb 10 '16 at 12:18
  • can you please open new question? it's very hard to help without any additional info. plus, this question is pretty old one... – rikica Feb 11 '16 at 14:07
  • @Son did you find the solution ? what is the purpose of application.properties file if i do like Rikica said ? – akinKaplanoglu Apr 14 '16 at 17:48
1

in this question I posted a full example how to unit test a spring controller which needs an autowired JpaRepository.

Heri
  • 4,368
  • 1
  • 31
  • 51