将 Spring Data JPA 与 Spring JPA 结合使用时出现问题
Issue in using Spring Data JPA with Spring JPA
问题
-
出现 war 文件部署错误。获取运行时异常。在一些附加信息之后提到了错误详细信息。
一些背景 -
1) 功能与 Spring JPA 库一起使用。当它迁移到 Spring Data JPA 时失败了。
2) 我正在使用 Spring Java 基于注释的配置来加载 bean。
3) 我有 2 个数据源。其中之一是使用数据源访问可以使用不同的 jar 文件检索的数据。此 jar 文件使用 Spring Data JPA v 1.10.2.RELEASE
4) 我们的应用程序使用 Spring 4.2.6 版本
实施-
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages =
"com.security.repository", entityManagerFactoryRef =
"entityManager")
public class PersistenceConfig {
@Bean(name = "dataSource")
public DataSource dataSource() throws NamingException {
return (DataSource) jndidataSource().getObject();
}
@Bean
public JndiObjectFactoryBean jndiDataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new
JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("dbDataSourceName");
jndiObjectFactoryBean.setExpectedType(DataSource.class);
return jndiObjectFactoryBean;
}
@Bean(name = "entityManager")
public LocalContainerEntityManagerFactoryBean entityManagerObj() throws
NamingException {
LocalContainerEntityManagerFactoryBean emf = new
LocalContainerEntityManagerFactoryBean();
emf.setJtaDataSource(jndiDataSource());
emf.setPersistenceUnitName("PersistenceUnit");
emf.setPackagesToScan("com.security.entity");
emf.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
emf.setPersistenceProvider(new PersistenceProvider());
emf.setJpaProperties(getJPAProperties());
return emf;
}
private Properties getJPAProperties() {
Properties jpaProperties = new Properties();
jpaProperties.put("eclipselink.weaving", "false");
jpaProperties.put("eclipselink.jpa.uppercase-column-names", "true");
jpaProperties.put("eclipselink.logging.level", "SEVERE");
jpaProperties.put("eclipselink.logging.parameters", "true");
jpaProperties.put("eclipselink.query-results-cache", "false");
jpaProperties.put("eclipselink.cache.shared.default", "false");
jpaProperties.put("eclipselink.cache.type.default ", "NONE");
jpaProperties.put("eclipselink.logging.level.sql", "SEVERE");
jpaProperties.put("eclipselink.jdbc.batch-writing", "JDBC");
jpaProperties.put("eclipselink.jdbc.batch-writing.size", "100");
return jpaProperties;
}
}
当使用上述配置部署应用程序时,出现以下错误。不确定缺少或不正确的内容。因为它正在使用 Spring JPA 而不是 Spring Data JPA 实现的库。请建议-
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#2': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[javax.persistence.EntityManagerFactory]: Could not convert factory method
argument value of type [weblogic.jdbc.common.internal.RmiDataSource] to
required type [javax.persistence.EntityManagerFactory]: Failed to convert
value of type [weblogic.jdbc.common.internal.RmiDataSource] to required type
[javax.persistence.EntityManagerFactory]; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[weblogic.jdbc.common.internal.RmiDataSource] 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#2': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[javax.persistence.EntityManagerFactory]: Could not convert factory method
argument value of type [weblogic.jdbc.common.internal.RmiDataSource] to
required type [javax.persistence.EntityManagerFactory]: Failed to convert
value of type [weblogic.jdbc.common.internal.RmiDataSource] to required type
[javax.persistence.EntityManagerFactory]; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[weblogic.jdbc.common.internal.RmiDataSource] to required type
[javax.persistence.EntityManagerFactory]: no matching editors or conversion
strategy found
通过从基于 Java 的 bean 配置移动到基于 XML 的配置,这个问题得到了解决。
问题
-
出现 war 文件部署错误。获取运行时异常。在一些附加信息之后提到了错误详细信息。
一些背景 -
1) 功能与 Spring JPA 库一起使用。当它迁移到 Spring Data JPA 时失败了。
2) 我正在使用 Spring Java 基于注释的配置来加载 bean。
3) 我有 2 个数据源。其中之一是使用数据源访问可以使用不同的 jar 文件检索的数据。此 jar 文件使用 Spring Data JPA v 1.10.2.RELEASE
4) 我们的应用程序使用 Spring 4.2.6 版本
实施-
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages =
"com.security.repository", entityManagerFactoryRef =
"entityManager")
public class PersistenceConfig {
@Bean(name = "dataSource")
public DataSource dataSource() throws NamingException {
return (DataSource) jndidataSource().getObject();
}
@Bean
public JndiObjectFactoryBean jndiDataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new
JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("dbDataSourceName");
jndiObjectFactoryBean.setExpectedType(DataSource.class);
return jndiObjectFactoryBean;
}
@Bean(name = "entityManager")
public LocalContainerEntityManagerFactoryBean entityManagerObj() throws
NamingException {
LocalContainerEntityManagerFactoryBean emf = new
LocalContainerEntityManagerFactoryBean();
emf.setJtaDataSource(jndiDataSource());
emf.setPersistenceUnitName("PersistenceUnit");
emf.setPackagesToScan("com.security.entity");
emf.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
emf.setPersistenceProvider(new PersistenceProvider());
emf.setJpaProperties(getJPAProperties());
return emf;
}
private Properties getJPAProperties() {
Properties jpaProperties = new Properties();
jpaProperties.put("eclipselink.weaving", "false");
jpaProperties.put("eclipselink.jpa.uppercase-column-names", "true");
jpaProperties.put("eclipselink.logging.level", "SEVERE");
jpaProperties.put("eclipselink.logging.parameters", "true");
jpaProperties.put("eclipselink.query-results-cache", "false");
jpaProperties.put("eclipselink.cache.shared.default", "false");
jpaProperties.put("eclipselink.cache.type.default ", "NONE");
jpaProperties.put("eclipselink.logging.level.sql", "SEVERE");
jpaProperties.put("eclipselink.jdbc.batch-writing", "JDBC");
jpaProperties.put("eclipselink.jdbc.batch-writing.size", "100");
return jpaProperties;
}
}
当使用上述配置部署应用程序时,出现以下错误。不确定缺少或不正确的内容。因为它正在使用 Spring JPA 而不是 Spring Data JPA 实现的库。请建议-
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#2': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[javax.persistence.EntityManagerFactory]: Could not convert factory method
argument value of type [weblogic.jdbc.common.internal.RmiDataSource] to
required type [javax.persistence.EntityManagerFactory]: Failed to convert
value of type [weblogic.jdbc.common.internal.RmiDataSource] to required type
[javax.persistence.EntityManagerFactory]; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[weblogic.jdbc.common.internal.RmiDataSource] 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#2': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[javax.persistence.EntityManagerFactory]: Could not convert factory method
argument value of type [weblogic.jdbc.common.internal.RmiDataSource] to
required type [javax.persistence.EntityManagerFactory]: Failed to convert
value of type [weblogic.jdbc.common.internal.RmiDataSource] to required type
[javax.persistence.EntityManagerFactory]; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[weblogic.jdbc.common.internal.RmiDataSource] to required type
[javax.persistence.EntityManagerFactory]: no matching editors or conversion
strategy found
通过从基于 Java 的 bean 配置移动到基于 XML 的配置,这个问题得到了解决。