7

I'm using Spring 3.1.1.RELEASE, Hibernate 4.1.0.Final, and JPA 2.0. Is there a way I can configure Spring transactions to commit after the transactions are executed without Java code? In other words, I would like to set flush mode to commit in either the application context file, hibernate configuration file, or persistence.xml file. My Spring transaction service class looks like

@Transactional(rollbackFor = Exception.class)
@Service
public class ContractServiceImpl implements ContractService
{

    @Autowired
    private ContractDAO m_contractDao;

    public void addContract(Contract contract)
    {
       m_contractDao.create(contract);
    }

    ...

and my application context is set up like so …

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:myproject" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml"/>
    <property name="persistenceUnitName" value="testingDatabase"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
   <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven />

My persistence.xml file is

<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="testingDatabase" transaction-type="RESOURCE_LOCAL">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <properties>
                        <property name="hibernate.ejb.cfgfile" value="/hsql_hibernate.cfg.xml" />
        <property name="org.hibernate.FlushMode" value="commit" />
                </properties>
        </persistence-unit>
</persistence>

and my hibernate config file is

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>

        <mapping class="org.mainco.subco.sbadmin.domain.Product" />
        <mapping class="org.mainco.subco.sbadmin.domain.Contract" />
        <mapping class="org.mainco.subco.organization.domain.Country" />
        <mapping class="org.mainco.subco.organization.domain.State" />
        <mapping class="org.mainco.subco.organization.domain.Address" />
        <mapping class="org.mainco.subco.organization.domain.OrganizationType" />
        <mapping class="org.mainco.subco.organization.domain.Organization" />

    </session-factory>
</hibernate-configuration>
Dave
  • 15,639
  • 133
  • 442
  • 830

4 Answers4

2

Try the following in your web.xml

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>flushMode</param-name>
        <param-value>COMMIT</param-value>
    </init-param>
</filter>

Reference.

bvulaj
  • 5,023
  • 5
  • 31
  • 45
  • Hi, With regards to this answer, I don't have a web.xml file. My Maven project builds a JAR. It uses Spring libraries to do transactions but doesn't have a web component. – Dave Nov 14 '12 at 23:35
1

As another option, you can configure the Hibernate EntityManager directly to use a particular flush mode by default using the org.hibernate.flushMode configuration setting.

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46
  • 1
    Where am I supposed to add that? I included that directive in my persistence.xml file (see question edit), but calls to "entityManager.getFlushMode()" within my DAO still return "auto". – Dave Nov 13 '12 at 22:44
  • You'd have to call into the Hibernate Session to determine the effect (that setting effects the underlying Session): `entityManager.unwrap( Session.class ).getFlushMode()` – Steve Ebersole Nov 13 '12 at 23:00
  • It should in retrospect, probably effect the local EntityManager flush mode as well. – Steve Ebersole Nov 13 '12 at 23:01
  • Yeah, I tried the alternative way you suggested of reading the flush mode and it still returned "AUTO." – Dave Nov 14 '12 at 23:39
1

Check this link

You may need to extend

org.springframework.orm.jpa.vendor.HibernateJpaDialect 

I hope this helps!

Dhananjay
  • 3,903
  • 2
  • 29
  • 44
0

I am not sure if this type of setting is available in spring. (I haven't seen one) But, as an alternative hibernate provides generic CRUD methods that you can use for all your classes if you pass them in as generics. Just put the call to the flush method in the Update/Create methods and use these exclusively to create/update all your classes.

Here is an example:

http://www.ibm.com/developerworks/java/library/j-genericdao/index.html

Jimmy Johnson
  • 889
  • 7
  • 20
  • Hi, Only b/c I specified "without Java code" in my question, I'm going to hold off on accepting your answer while I search around. Thx for the example, tho. – Dave Nov 07 '12 at 01:01