6

我们有一个在服务器上使用 Spring 容器的应用程序,该容器使用 EJB3 实体和 Hibernate 将数据持久保存在 PostgreSQL 数据库中。

我想使用单独的 EntityManager 添加另一个到单独数据库的连接,但 DBMS (Trifox Vortex) 的供应商没有DataSource类型驱动程序。

Hibernate EntityManager 可以使用简单的 JDBC 兼容驱动程序来实现实体的 EJB3 JPA 持久性吗?

我是否需要为要连接的 DBMS 提供特定的 Hibernate 方言?

值得一提的是,这是我们当前在 Spring 中的 Entity Manager Factory 的 XML 定义:

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="UniWorks-EntityPersistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    </property>
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="${db.showsql}"/>
        <property name="generateDdl" value="${db.generate}"/>
        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
      </bean>
    </property>
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl}</prop>
      </props>
    </property>
  </bean>

更新/进展: 21/3/13

在与供应商反复讨论之后,我设法让他们的 JDBC 驱动程序与一个简单的 Java 测试程序一起工作。

不幸的是,他们的 JDBC 驱动程序显然不足以让 Hibernate 获得足够的信息,因为 Spring 无法使用以下堆栈跟踪创建 EntityManagerFactory:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [bundle://222.0:0/META-INF/spring/cobol.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: UniWorks-CobolPersistenceUnit] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)[59:org.springframework.context:3.1.1.RELEASE]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)[90:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)[90:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)[90:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)[90:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)[91:org.springframework.osgi.extender:1.2.1]
    at java.lang.Thread.run(Thread.java:662)[:1.6.0_24]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: UniWorks-CobolPersistenceUnit] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)[64:org.springframework.orm:3.1.1.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)[64:org.springframework.orm:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)[58:org.springframework.beans:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)[58:org.springframework.beans:3.1.1.RELEASE]
    ... 14 more
Caused by: org.hibernate.HibernateException: Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use
    at org.hibernate.dialect.resolver.DialectFactory.determineDialect(DialectFactory.java:141)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:97)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:117)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)[80:com.springsource.org.hibernate:3.3.2.GA]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)[80:com.springsource.org.hibernate:3.3.2.GA]
    ... 19 more
Caused by: java.sql.SQLException: getDatabaseMajorVersion: Not supported by VORTEXjdbc.
    at vortex.sql.vortexDbMetaData.getDatabaseMajorVersion(vortexDbMetaData.java:362)
    at org.hibernate.dialect.resolver.DialectFactory.determineDialect(DialectFactory.java:131)[80:com.springsource.org.hibernate:3.3.2.GA]
    ... 26 more

所以看起来我可能不得不编写自己的方言子类。任何人都给我任何关于从哪里开始寻找或扩展现有子类的指示?

4

2 回答 2

3

这应该有效(SimpleDriverDataSource + org.springframework.orm.jpa.vendor.Database.DEFAULT):

   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="org.h2.Driver"/> <!-- put your driver here -->
        <property name="url" value="jdbc:h2:mem:test"/> <!-- put your jdbc url here -->
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

            <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="TestPersistenceUnit"/>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false"/>
                <property name="database" value="DEFAULT"/>
            </bean>
        </property>
    </bean>
于 2013-03-11T14:38:23.670 回答
0

Hibernate 可以使用 DataSource 以外的连接池。所以你可以使用 c3p0 或 proxool 或 dbcp 甚至你自己的连接池。Hibernate 确实有一个内置的连接池,尽管它仅用于测试/原型设计;它只允许您传入驱动程序名称、url、用户名、密码,它会设置一些(非常)简单的池。

有几种不同的选择可以实现这一目标。首先,Hibernate 总是通过统一的合约来处理连接的获取和释放:org.hibernate.engine.jdbc.connections.spi.ConnectionProvider. 因此,一种选择是传入您自己的 ConnectionProvider 实现。

另一种选择是传入驱动程序、url、用户名、密码和要使用的 ConnectionProvider。对于驱动程序、url、用户名和密码,Hibernate 和 JPA 都定义了执行此操作的机制。对于 JPA ,您可以使用 persistence.xml 或设置javax.persistence.jdbc.driverjavax.persistence.jdbc.url和. 至于 ConnectionProvider,默认情况下,Hibernate 将选择不受支持、不用于生产的那个。但是要告诉 Hibernate 使用不同的设置,您将使用该设置。Hibernate 内置支持与 c3p0 和 proxool 集成作为后端连接池(Hibernate 将为您设置和管理 c3p0/proxool 池并使用该池进行连接管理)。javax.persistence.jdbc.userjavax.persistence.jdbc.passwordhibernate.connection.provider_class

正如另一个答案指出的那样,另一种选择是在驱动程序和连接信息周围使用 DataSource 包装器。

抱歉,我不了解 Spring,所以我不知道所有特定于 Spring 的方法来指定 persistence.xml 或 JPA 设置或 Hibernate 设置。

于 2013-03-16T16:09:28.200 回答