我正在編寫一個模塊來保存一些數據,並使用OpenJPA
作爲持久性提供程序。但是現在我想給出一個配置選項供用戶在OpenJPA
和hibernate
之間切換。於是我寫了一個新的DaoFactory類來創建EntityManagerFactory
並在persistence.xml
中爲hibernate創建了新的持久性單元(My-PU-Hibernate
)。在OSGI環境中同時使用OpneJPA和Hibernate
現在,當我創建EntityManagerFactory時會發生問題。我的代碼,
Persistence.createEntityManagerFactory("My-PU-Hibernate", hibernatePropertyMap);
我的persistence.xml
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="My-PU-OpenJPA" transaction-type="RESOURCE_LOCAL">
<class>org.example.testproject.jpa.openjpa.entity.AttachmentDAOImpl</class>
</persistence-unit>
<persistence-unit name="My-PU-Hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.example.testproject.jpa.openjpa.entity.AttachmentDAOImpl</class>
</persistence-unit>
</persistence>
我Hibernate的屬性地圖,
"hibernate.connection.datasource", datasource
"hibernate.dialect", "org.hibernate.dialect.MySQLDialect"
"hibernate.hbm2ddl.auto","update"
"hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"
"hibernate.cache.use_query_cache", "false"
"hibernate.cache.use_second_level_cache", "false"
此挑選OpneJPA PersistenceProvider
爲正確的持久性提供和創建了一個從EntityManagerFactory的OpenJPA持久性提供者。它在創建EntityManagerFactory時也會發出以下警告。
WARN [Start Level Event Dispatcher] openjpa.Runtime - The configuration property named "openjpa.Id" was not recognized and will be ignored, although the name closely matches a valid property called "openjpa.Id".
所以,當我嘗試創建一個實體管理器,它會給出以下錯誤。
<openjpa-2.2.0-wso2v1-r128166:129108 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property. Available properties in configuration are "[email protected]".
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:844)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:602)
at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1510)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:518)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:443)
at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:104)
at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:968)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:959)
at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:638)
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:203)
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60)
我覺得上面的警告和錯誤發生是由於選擇了錯誤的PersistenceProvider。我放棄了代碼,並驗證了也在OSGI環境中加載了hibernate持久性提供程序。我使用geronimo-jpa_2.0_spec
v1.0
作爲javax.persistence供應商。
關於如何選擇正確的持久性提供者的想法?