Spring Hibernate Transaction Management Project

Click here to download eclipse supported ZIP file



This is PersonDao.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.hibernate.tx.dao;

import com.cv.spring.hibernate.tx.model.Person;

/**
 @author Chandra Vardhan
 */
public interface PersonDao {
  
  void insertUser(Person person);

  void deletePerson(int personID);

  void selectAllPerson();

  void selectPersonByName();

}


This is PersonDaoImpl.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.hibernate.tx.dao;


import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.cv.spring.hibernate.tx.model.Person;

/**
 @author Chandra Vardhan
 */
@Repository
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
  
  private final static Logger LOGGER = Logger.getLogger(PersonDaoImpl.class);
  
  @Autowired
  private SessionFactory sessionFactory;
  

  @Override
  public void insertUser(Person person) {
    LOGGER.info("Control entered into PersonDaoImpl.insertUser(-) method!!!");    
    Session openSession = sessionFactory.openSession();
    Transaction tx = openSession.beginTransaction();
    openSession.save(person)
    tx.commit();
    LOGGER.info("Person table inserted with ");
    LOGGER.info("Control exited PersonDaoImpl.insertUser(-) method!!!");
  }

  @Override
  public void deletePerson(int personID) {
    getHibernateTemplate().delete(personID);
  }

  @Override
  public void selectAllPerson() {

  }

  @Override
  public void selectPersonByName() {

  }

}


This is Person.java file having the source code to execute business logic.


 

    
package com.cv.spring.hibernate.tx.model;

import java.io.Serializable;

/**
 @author Chandra Vardhan
 */
public class Person implements Serializable {

  private long id;
  private String firstName;
  private String lastName;
  private String street;
  private String city;
  private String state;
  private String country;

  public Person(String firstName, String lastName, String street,
      String city, String state, String country) {
    super();
    if (firstName != null) {
      int val = firstName.hashCode();
      if (val != && val < 10000) {
        this.id = val;
      else {
        val = val / 10000;
        this.id = val;
      }
    }
    this.firstName = firstName;
    this.lastName = lastName;
    this.street = street;
    this.city = city;
    this.state = state;
    this.country = country;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getFirstName() {
    return this.firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getStreet() {
    return street;
  }

  public void setStreet(String street) {
    this.street = street;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getState() {
    return state;
  }

  public void setState(String state) {
    this.state = state;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

}


This is TestMainClass.java file having the source code to execute business logic.


 

    
package com.cv.spring.hibernate.tx;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cv.spring.hibernate.tx.dao.PersonDao;
import com.cv.spring.hibernate.tx.model.Person;

/**
 @author Chandra Vardhan
 */
public class TestMainClass {

  public static void main(String[] argsthrows Exception {

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        "beanDefinition.xml");

    PersonDao personDAO = applicationContext.getBean("personDao",
        PersonDao.class);
    String firstName = "Chandra";
    Person person = new Person(firstName, "Vardhan""Kukat""Hy""TG",
        "IN");

    // Insert user to the table 3 times:
    personDAO.insertUser(person);

    // Delete first person from table
    // personDAO.deletePerson(val);

    // Select all inserted user from the table
    // personDAO.selectAllPerson();

    // Select data from tabel where person
    // name is "Java"
    // personDAO.selectPersonByName();

    applicationContext.close();

  }

}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.hibernate.tx</groupId> <artifactId>SpringHibernateTransactionManagement</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>SpringHibernateTransactionManagement</name> <properties> <springframework.version>4.2.0.RELEASE</springframework.version> <springsecurity.version>4.0.1.RELEASE</springsecurity.version> <log4j-version>1.2.17</log4j-version> </properties> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${springsecurity.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${springsecurity.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <artifactId>hibernate-core</artifactId> <groupId>org.hibernate</groupId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> <finalName>SpringHibernateTransactionManagement</finalName> </build> </project>


This is beanDefinition.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cv.spring.hibernate.tx" />
<!-- Database Configuration -->
<import resource="datasource.xml" />
<import resource="hibernate.xml" />
<!-- Beans Declaration -->
<import resource="transaction.xml" />
<import resource="person.xml" />
</beans>


This is datasource.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:annotation-config />
<context:component-scan base-package="com.cv.spring.hibernate.tx" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>


This is hibernate.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:annotation-config />
<context:component-scan base-package="com.cv.spring.hibernate.tx" />
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<!-- <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property> -->

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>/Person.hbm.xml</value>
</list>
</property>
</bean>
</beans>


This is Person.hbm.xml file having the spring configuration properties.


 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 29, 2010 1:16:19 PM by Hibernate Tools 3.2.5.Beta -->
<hibernate-mapping>
<class name="com.cv.spring.hibernate.tx.model.Person" table="PERSON">
<id name="id" type="java.lang.Long">
<column name="ID" precision="10" scale="0" />
<generator class="increment" />
</id>
<property name="firstName" type="java.lang.String">
<column name="first_name" />
</property>
<property name="lastName" type="java.lang.String">
<column name="last_name" />
</property>
<property name="street" type="java.lang.String">
<column name="street_name" />
</property>
<property name="city" type="java.lang.String">
<column name="city" />
</property>
<property name="state" type="java.lang.String">
<column name="state" />
</property>
<property name="country" type="java.lang.String">
<column name="country" />
</property>
</class>
</hibernate-mapping>


This is person.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:annotation-config />
<context:component-scan base-package="com.cv.spring.hibernate.tx" />
<!-- Product business object -->
<bean id="personDaoImpl" class="com.cv.spring.hibernate.tx.dao.PersonDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="personDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="personDaoImpl" />
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>


This is transaction.xml file having the spring configuration properties.


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cv.spring.hibernate.tx" />
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- <property name="transactionAttributes" ref="txAdvice"/> -->
<property name="transactionAttributes">
<props>
<prop key="save">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- <tx:advice> <tx:attributes> all methods starting with 'get' are read-only
<tx:method name="get*" read-only="true" /> <tx:method name="*" /> </tx:attributes>
</tx:advice> -->
<!-- ensure that the above transactional advice runs for any execution of
an operation defined by the FooService interface -->
<!-- <aop:config> <aop:pointcut id="personDAO" expression="execution(* com.cv.spring.hibernate.tx.PersonDAO.*(..))"
/> <aop:advisor advice-ref="txAdvice" pointcut-ref="personDAO" /> </aop:config> -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
the transactional semantics... <tx:attributes> all methods starting with
'get' are read-only <tx:method name="update" propagation="REQUIRED" isolation="DEFAULT"
read-only="false" /> other methods use the default transaction settings (see
below) </tx:attributes> </tx:advice> -->
</beans>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


No comments:

Post a Comment