我正在编写一个模块来持久化一些数据并用作OpenJPA
持久性提供程序。但现在我想为用户提供一个配置选项以在OpenJPA
和之间切换hibernate
。所以我写了一个新的 DaoFactory 类来为休眠创建EntityManagerFactory
和创建新的持久性单元(My-PU-Hibernate
) 。persistence.xml
现在,当我创建 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.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"
这会选择 OpneJPAPersistenceProvider
作为正确的持久性提供程序,并从 OpenJPA 持久性提供程序创建一个 EntityManagerFactory。它还在创建 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 "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@442ce698".
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 环境中。我用作geronimo-jpa_2.0_spec
v1.0
javax.persistence 供应商。
关于如何选择正确的持久性提供程序的任何想法?