0

My app using spring and jpa, i have two config file:

AppConfig:

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.test.api" })
@EnableJpaRepositories("com.test.api.repository")
@PropertySource("classpath:application.properties")
public class AppConfig { 
}

WebAppConfig:

@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = { "com.test.webservice" })
  public static class WebAppConfig extends WebMvcConfigurerAdapter {

  }

Almost work fine, but sometime it throw an exception: Caused by: java.lang.IllegalStateException: No transactional EntityManager available even I just select record (Not create, update or delete data.)

If I move annotation: @ComponentScan(basePackages = { "com.test.api" }) to Class WebAppConfig and remove: @ComponentScan(basePackages = { "com.test.webservice" }) then exception is disappear, but spring context load many time when server startup and spring beans are duplicate.

Or If I use entityManager.getEntityManagerFactory().createEntityManager().unwrap(Session.class) instead for entityManager.unwrap(Session.class) then exception also disappear.

How can I solve it?

namtn
  • 71
  • 1
  • 6

1 Answers1

0

Remove the ComponentScan on WebAppConfig and add the package in the ComponentScan annotation on AppConfig, that should solve your problem.

dunni
  • 43,386
  • 10
  • 104
  • 99
  • Could you then post your code, where you configure the EntityManager? Maybe there is an error in that. – dunni Apr 03 '15 at 06:50