Spring Hibernate Transaction Project

Click here to download eclipse supported ZIP file



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


 

    
package com.cv.spring.hb.dao;

import java.util.List;

import com.cv.spring.hb.model.User;
/**
 @author Chandra Vardhan
 */
public interface UserDAO {

  void insertUser(User user);
  
  User getUserById(int userId);
  
  User getUser(String username);
  
  List<User> getUsers();
}


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


 

    
package com.cv.spring.hb.dao;


import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cv.spring.hb.model.User;
/**
 @author Chandra Vardhan
 */
@Service
public class UserDAOImpl implements UserDAO {
  
  private final static Logger LOGGER = Logger.getLogger(UserDAOImpl.class);

  @Autowired
  private SessionFactory sessionFactory;
  
  
  
  @Override
  public void insertUser(User user) {
    sessionFactory.getCurrentSession().saveOrUpdate(user);
  }

  @Override
  public User getUserById(int userId) {
    return (UsersessionFactory.getCurrentSession().get(User.class, userId);
  }
  
  @Override
  public User getUser(String username) {
    
    Query query = sessionFactory.getCurrentSession().createQuery("from User where username = :username");
    
    LOGGER.info(query);
    query.setParameter("username", username);
    return (Userquery.list().get(0);
  }
  
  @Override
  public List<User> getUsers() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
    return criteria.list();
  }

}


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


 

    
package com.cv.spring.hb;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cv.spring.hb.model.User;
import com.cv.spring.hb.service.UserService;

/**
 @author Chandra Vardhan
 */
public class MainProgram {
  private final static Logger LOGGER = Logger.getLogger(MainProgram.class);

  public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "spring.xml");
    UserService userManager = (UserServicectx.getBean("userServiceImpl");

    User user = new User();

    user.setUsername("chandra");
    
    user.setName("kodam");

    userManager.insertUser(user);

    LOGGER.info("User inserted!");

  }
}


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


 

    
package com.cv.spring.hb.model;

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

  private int id;
  
  private String username;
  
  private String name;
  
  public int getId() {
    return id;
  }
  
  public void setId(int id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }
  
  public void setUsername(String username) {
    this.username = username;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
}


This is UserService.java file having the service/business logic to call the DAO layer and get the information from database.


 

    
package com.cv.spring.hb.service;

import java.util.List;

import com.cv.spring.hb.model.User;
/**
 @author Chandra Vardhan
 */
public interface UserService {

  void insertUser(User user);
  
  User getUserById(int userId);
  
  User getUser(String username);
  
  List<User> getUsers();
}


This is UserServiceImpl.java file having the service/business logic to call the DAO layer and get the information from database.


 

    
package com.cv.spring.hb.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cv.spring.hb.dao.UserDAO;
import com.cv.spring.hb.model.User;
/**
 @author Chandra Vardhan
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDAO userDAO;
  
  @Override
  @Transactional
  public void insertUser(User user) {
    userDAO.insertUser(user);
  }

  @Override
  @Transactional
  public User getUserById(int userId) {
    return userDAO.getUserById(userId);
  }
  
  @Override
  @Transactional
  public User getUser(String username) {
    return userDAO.getUser(username);
  }

  @Override
  @Transactional
  public List<User> getUsers() {
    return userDAO.getUsers();
  }

}




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">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.hb</groupId> <artifactId>SpringHibernateTransaction</artifactId> <name>SpringHibernateTransaction</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <org.springframework-version>4.2.0.RELEASE</org.springframework-version> <org.aspectj-version>1.6.10</org.aspectj-version> <org.slf4j-version>1.6.6</org.slf4j-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-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.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.4.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.0.CR2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> test </dependency> </dependencies> <build> <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> </build> </project>


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


 
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<context:component-scan base-package="com.cv.spring.hb" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/hibernate" />
<property name="username" value="postgres" />
<property name="password" value="password" />
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>user.hbm.xml</value>
</list>
</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>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="mysessionFactory">
</bean>

</beans>


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


 
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cv.spring.hb.model.User" table="user_new" >
<id name="id">
<generator class="assigned"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="username" column="username" type="java.lang.String"/>
</class>
</hibernate-mapping>



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