jeudi 2 avril 2015

No transactional EntityManager available;

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 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?


Aucun commentaire:

Enregistrer un commentaire