Step-1
Add dependency inside the pom.xml
<!-- Dependency for hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
</dependencies>
Step-2
<!-- Hibernate 4 with spring 3.x -->
<bean id="librarySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="libraryDataSource" />
<property name="packagesToScan">
<list>
<value>com.usc.dao.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- below key is important use this key = hibernate.hbm2ddl.auto not
hbm2ddl.auto -->
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Step-3
Define the datasource
<context:property-placeholder location="/WEB-INF/context/persistence-mysql.properties" />
<bean id="libraryDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="singleton">
<property name="driverClassName" value="${db.driverclassname}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
Step-4
"/WEB-INF/context/persistence-mysql.properties
################Comment###############
#db.driverclassname=com.mysql.jdbc.Driver
#db.url=jdbc:mysql://localhost:3306/library_db?createDatabaseIfNotExist=true
#db.username=root
#db.password=root
########Setting for HSQL##################
db.driverclassname=org.hsqldb.jdbc.JDBCDriver
db.url=jdbc:hsqldb:mem:library_db?createDatabaseIfNotExist=true
db.username=sa
db.password=
Step-5
Define Spring Transaction Manager
<!--
@Transactional
-->
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="librarySessionFactory"></property>
</bean>
Step-6
Applying transaction manager in dao layer
/**
*
* @author usc
*
*/
@Repository("IUserDao")
@Transactional(propagation=Propagation.REQUIRED)
public class IUserDao extends AbstractDaoImpl<UserEntity,Integer> implements UserDao {
}
AbstractDaoImpl.java
/**
* @author usc
* @param <E>
* @param <I>
*/
public abstract class AbstractDaoImpl<E, I extends Serializable> implements AbstractDao<E, I> {
private Class<E> entityClass;
protected AbstractDaoImpl(Class<E> entityClass) {
this.entityClass = entityClass;
}
@Autowired
@Qualifier("librarySessionFactory")
private SessionFactory psessionFactory;
public Session getCurrentSession() {
return psessionFactory.getCurrentSession();
}
}