Announcement Announcement Module
No announcement yet.
Hibernate4 JPA and JDBC Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate4 JPA and JDBC

    We want to migrate from hibernate 3 to hibernate 4. The main reason being the envers integration.

    Part of our application performs jdbc calls via iBatis (via an external bpm product: activiti),
    the other part access the database via jpa.

    Everything worked fine with hibernate 3.

    I guess we have to change something in the configuration in order to make it work.

    This the pertinent part of the config
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="database" value="db2"/>
                    <property name="databasePlatform" value=""/>
            <property name="jpaProperties">
                    <prop key="hibernate.dialect"></prop>
                    <prop key="">false</prop>
            <property name="persistenceXmlLocation" value="classpath:META-INF/foo-persistence.xml"/>
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/foo"/>
            <property name="resourceRef" value="true"/>
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
    and the foo-persistence.xml
     <persistence-unit name="foo" transaction-type="RESOURCE_LOCAL">
    What I could observe
    Activiti checks the database by calling these 2 methods one after the other:
      In Class org.activiti.engine.impl.db.DbSqlSession
      DatabaseMetaData databaseMetaData = connection.getMetaData();  // line 689
      tables = databaseMetaData.getTables(..)   // line 708
    The getTables returns null, hiding a NullPointerException thrown in org.hibernate.engine.jdbc.internal.proxy.Connectio nProxyHandler#getResourceRegistry.

    The connection.getMetaData() goes through org.springframework.jdbc.datasource.TransactionAwa reDataSourceProxy.TransactionAwareInvocationHandle r#invoke
    where a connection is retrieved and released.

    The connection (logicalConnection) is hold in the org.hibernate.engine.jdbc.internal.proxy.Connectio nProxyHandler.

    The getTables will try to retrieve the connection from the ConnectionProxyHandler, which will fail with a NullPointerException.

    I could workaround the problem by patching org.hibernate.engine.jdbc.internal.proxy.Connectio nProxyHandler#continueInvocation.
    The result of method (getMetaData()) is wrapped in the ConnectionProxyHandler. By removing the wrapping I got back the expected behavior (Well I'm not sure of that as I don't know why it was wrapped in the first place)

       Object result = method.invoke(extractPhysicalConnection(), args);
       // result = postProcess(result, proxy, method, args); <-- patch, commented : line 139
    I can't live for long with a patched Hibernate version.
    My guess is that I have to change something in the config.
    I tried a few things, but I don't have any more ideas

    The question
    The final question how can I configure hibernate 4 so that my use case works?


    Spring version: 3.1.1
    Hibernate version: 4.1.4
    Tomcat 7.0.23
    Last edited by fan; Jul 13th, 2012, 02:49 AM.

  • #2
    I found it out. The problem is how the
    class was initialised.
    method was called with value 'true'
    I just let the default value and everything works fine