獲得此配置的任何幫助都是值得歡迎的。Spring Boot voodoo需要使用DataNucleus和Hikari實例化JPA
我想從Spring Boot
接管自動連接池,數據源和JPA配置,以允許我將DataNucleus
加入混合而不是Hibernate
。
我的方法是代碼片斷Boot
說在試驗和錯誤的基礎上失蹤。我不得不刪除Hibernate的依賴關係以允許DataNucleus運行。
也許我現在編碼了太多,或許我不夠遠。
春翻倒出現錯誤:
Exception encountered during context initialization - cancelling refresh attempt:
[huge SNIP]
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.data.repository.support.Repositories]:
Factory method 'repositories' threw exception;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'symbolRepositoryImpl':
Unsatisfied dependency expressed through field 'entityManager';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'javax.persistence.EntityManager' available:
expected single matching bean but found 2:
org.springframework.orm.jpa.SharedEntityManagerCreator#0,
org.springframework.orm.jpa.SharedEntityManagerCreator#1
[SNIP]
2017-06-01 09:43:09.675 ERROR 9108 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter
***************************
APPLICATION FAILED TO START
***************************
Description:
Field entityManager in com.bp.gis.tardis.repository.SymbolRepositoryImpl
required a single bean, but 2 were found:
- org.springframework.orm.jpa.SharedEntityManagerCreator#0: defined by method 'createSharedEntityManager' in null
- org.springframework.orm.jpa.SharedEntityManagerCreator#1: defined by method 'createSharedEntityManager' in null
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans,
or using @Qualifier to identify the bean that should be consumed
我可以花時間調試這個進一步,但斷點進來裏面應該有一個EntityManager注入的庫之一的初始化。
這就是我手動實例:
@Configuration
@EnableJpaRepositories(
basePackages = {"org.adam.repository"}
)
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "adam.datasource")
public AdamDataSourceProperties getDataSourceProperties() {
return new AdamDataSourceProperties();
}
@Bean
public DataSource getDataSource() {
AdamDataSourceProperties props = getDataSourceProperties();
return new HikariDataSource(props.getHikariConfig());
}
@Bean
public LocalContainerEntityManagerFactoryBean getEmfBean() {
LocalContainerEntityManagerFactoryBean emfBean =
new LocalContainerEntityManagerFactoryBean();
emfBean.setDataSource(getDataSource());
emfBean.setPersistenceUnitName("adam");
return emfBean;
}
@Bean
public EntityManagerFactory getEmf() {
LocalContainerEntityManagerFactoryBean emfBean = getEmfBean();
return emfBean.getNativeEntityManagerFactory();
}
}
我AdamDatasourceProperties
被Spring使用「adam.datasource」初始化前綴值application.properties
,然後它可以創建一個HikariConfig
對象使用實例HikariDataSource
。這一點實際上很好,它可能是造成問題的實體管理器工廠 - 或其他。
我沒有證據表明我最後的方法getEmf()
實際上有幫助。
而且,我懷疑是錯誤
Required a single bean, but 2 were found
或建議的行動是有益的 - 我不喜歡進入春季的源代碼,以註釋的那些方法之一Spring的SharedEntityManagerCreator
爲@Primary
。
更新,如果發現在類路徑上其他JPA API類 DataNucleus
將不會運行 - 它堅持它自己版本的持久性API的 - 因此消除了Hibernate的包裝是必要的。
Caused by: org.datanucleus.exceptions.NucleusUserException:
Found Meta-Data for class org.adam.entity.TimeSeriesEntity
but this class is either not enhanced or you have multiple copies
of the persistence API jar in your CLASSPATH!!
Make sure all persistable classes are enhanced before running
DataNucleus and/or the CLASSPATH is correct.
at org.datanucleus.metadata.MetaDataManagerImpl
.initialiseClassMetaData(MetaDataManagerImpl.java:2814)
所以我已經從spring-boot-starter-data-jpa
排除休眠,錯誤消失。
不知道所有那些Spring的東西,但沒有LocalContainerEntityManagerFactoryBean.setPersistenceProviderClass定義您的JPA提供程序,因此也許您不必從CLASSPATH中排除其他提供程序?畢竟,DataNucleus完全遵從JPA規範中的「提供者」。 –
什麼是「它堅持自己的持久化API版本」?你在這裏沒有任何版本的東西。我沒有在他們的「API jar」中看到任何不在JPA 2.1規範中的東西。顯然,JPA專家組不會發布標準的JPA API,因此提供者都創建了自己的API jar。 –
@NeilStockton DataNucleus不必擁有一個persistence.xml,是嗎? – Adam