Switching Between Hibernate’s and Spring’s Transaction

Uncomment the below lines to hand the activation down to Spring

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tugrulaslan</groupId>
    <artifactId>WebApp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>BlogWebApp Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project-java.version>1.7</project-java.version>
        <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
        <junit.version>4.11</junit.version>
        <mysql-connector.version>5.1.34</mysql-connector.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <javax-persistance-api.version>1.0.2</javax-persistance-api.version>
        <spring.version>4.0.6.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>BlogWebApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${project-java.version}</source>
                    <target>${project-java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:property-placeholder location="classpath:hibernate.properties"/>

    <!-- Hibernate connection configuration -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${orm.connection.driver_class}"/>
        <property name="url" value="${orm.connection.url}"/>
        <property name="username" value="${orm.connection.username}"/>
        <property name="password" value="${orm.connection.password}"/>
    </bean>

    <!-- Hibernate configuration settings -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.tugrulaslan.entity"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${orm.dialect}</prop>
                <prop key="hibernate.show_sql">${orm.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${orm.hbm2ddl.auto}</prop>
              <!-- Enabling this will trade transaction management with Hibernate--> 
                <prop key="current_session_context_class">thread</prop>
            </props>
        </property>
    </bean>

    <!-- Spring Transaction Management
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/> -->

    <context:component-scan base-package="com.tugrulaslan"/>
</beans>

hibernate.properties

orm.connection.driver_class = com.mysql.jdbc.Driver
orm.connection.url = jdbc:mysql://localhost:3306/app
orm.connection.username = root
orm.connection.password = root
orm.dialect = org.hibernate.dialect.MySQLDialect
orm.show_sql = true
orm.hbm2ddl.auto = create

DAO

public interface AppDAO {
 public void saveObject(Object object);
}

DAOImpl

@Repository
public class AppDAOImpl implements AppDAO {

    @Autowired
    private SessionFactory sessionFactory;
@Override
    public void saveObject(Object object) {
        
        //Spring's session transaction management use
        //sessionFactory.getCurrentSession().saveOrUpdate(object);
        
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            session.saveOrUpdate(object);
            transaction.commit();
        } catch (HibernateException e) {
            System.err.println(e);
            transaction.rollback();
        } finally {
            session.close();
        }
    }

Service

public interface AppService {
void saveObject(Object object);
}

ServiceImpl

@Service
public class AppServiceImpl implements AppService {
@Override
    //@Transactional
    public void saveObject(Object object) {
        appDAO.saveObject(object);
    }
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *