Nick Nick - 10 days ago 7
Java Question

Issue after upgrading spring-data-jpa 1.9.4 to 1.10.2

I'm facing some bean creation issue after upgrading spring-data-jpa from 1.9.4 to 1.10.2 in wildfly server. Following is my exception stacktrace

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jpaContext': Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Set]: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#1': Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: Could not convert factory method argument value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: Failed to convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: no matching editors or conversion strategy found; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#1': Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: Could not convert factory method argument value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: Failed to convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: no matching editors or conversion strategy found
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#1': Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: Could not convert factory method argument value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: Failed to convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 28 more


The following is my @Configuration for EntityManagerFactory and Spring Data Jpa Repository

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class TransactionConfiguration {

@Bean
public JndiObjectFactoryBean dataSource() throws IllegalArgumentException {
final JndiObjectFactoryBean dataSource = new JndiObjectFactoryBean();
dataSource.setExpectedType(DataSource.class);
dataSource.setJndiName(environment.getProperty("default.jdbc.jndi.dataSource"));
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
return new JtaTransactionManager();
}

@Bean
public EntityManagerFactory entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setJtaDataSource(dataSource());
entityManagerFactory.setPersistenceUnitName("MyPersistenceUnit");
entityManagerFactory.setPackagesToScan(new String[] { "org.mycom.**.entity" });
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdaper());
entityManagerFactory.setJpaPropertyMap(additionalProperties());
entityManagerFactory.setValidationMode(ValidationMode.NONE);
entityManagerFactory.setSharedCacheMode(SharedCacheMode.DISABLE_SELECTIVE);
ClasspathScanningPersistenceUnitPostProcessor hbmScanner = new ClasspathScanningPersistenceUnitPostProcessor("com.mycomp");
hbmScanner.setMappingFileNamePattern("**/*hbm.xml");
entityManagerFactory.setPersistenceUnitPostProcessors(hbmScanner);
entityManagerFactory.afterPropertiesSet();
return entityManagerFactory.getObject();
}
}


and my repository config as follows

@Configuration
@EnableJpaRepositories(basePackages="org.mycom.**.repository",
repositoryFactoryBeanClass=EnversRevisionRepositoryFactoryBean.class)
@EnableJpaAuditing
public class RepositoryConfiguration {

@Autowired
private SecurityUtils securityUtils;

@Bean
@Profile("production")
public AuditorAware<User> springSecurityAwareAuditor() {
return () -> securityUtils.getCurrentUser();
}


}

Answer

That's a bug in Spring Data JPA, which is fixed for Ingalls RC1 and Hopper SR3 (incl. in Spring Boot 1.4.1).

The workaround is to declare your JNDI objects in an XML configuration file and explicitly set the expected-type attribute.

Comments