0

In order to work around Hibernate bug HHH-2763, I'm trying to update my app from Hibernate 3 to Hibernate 4. It seemed to have gone smoothly until I realized that while my application can read data, it never seems to do inserts or updates. I turned on SQL logging: under Hibernate 3, there are inserts and updates. Under Hibernate 4, there are no inserts and updates.

We were doing explicit flushes in Hibernate 3 by overriding the OpenSessionInViewFilter class' closeSession method as follows:

public void closeSession(Session session, SessionFactory sessionFactory) {
    session.flush();
    super.closeSession(session, sessionFactory);
}

But in Hibernate 4, this is no longer an option because that method no longer exists.

My Hibernate 4 configuration for the Session Factory and Transaction Manager follows:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.default_schema">${oracle.default_schema}</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            <prop key="hibernate.show_sql">${hibernate.showSql}</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
    <property name="mappingResources">
         <list> . . . </list>
    </property>
    <property name="annotatedClasses">
         <list> . . . </list>
    </property>
</bean>

<!-- Configure transaction management, enabling @Transactional annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="nestedTransactionAllowed" value="true" />
</bean>

(Edit) And here's the configuration of the OpenSessionInViewFilter:

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>

My guess is that it's not flushing and committing. But why?

Marvo
  • 17,845
  • 8
  • 50
  • 74
  • 1
    Wt about your transaction interface? You should change update property in your@transactional annotation. – Kanagaraj M Jul 26 '13 at 02:55
  • For better or for worse, we explicitly specify `readOnly=true` or `readOnly=false` with each `@Transactional` annotation. Is that what you mean? – Marvo Jul 26 '13 at 17:28

1 Answers1

0

what do you mean by "But in Hibernate 4, this is no longer an option because that method no longer exists"? spring comes with a OpenSessionInViewFilter for hibernate 4, which is:

org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

are you still using?

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

try the new OpenSessionInViewFilter for hibernate 4

edit...

You do not have to flush the session manually, transcation manager will take care of it, as the javadoc of hibernate4 OpenSessionInViewFilter says:

The active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction.

you can change you log level to TRACE, and check the console to make sure flush mode is set to AUTO:

... setting flush mode to: AUTO

or can you post your save/update code fragment?

Septem
  • 3,614
  • 1
  • 20
  • 20
  • Sorry, I wasn't clear. You can no longer override the `closeSession` method in OpenSessionInViewFilter because it doesn't exist or isn't public. We're using the hibernate4 version of this filter. http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.html – Marvo Jul 26 '13 at 17:24