add this dependancy
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!-- end hibernate -->
and then change spring configuration like this
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>/WEB-INF/classes/hibernate/**/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class"> ${hibernate.connection.driver_class}</prop>
<prop key="hibernate.connection.url"> ${hibernate.connection.url}</prop>
<prop key="hibernate.connection.username"> ${hibernate.connection.username}</prop>
<prop key="hibernate.connection.password"> ${hibernate.connection.password}</prop>
<!-- <prop key="hibernate.connection.internal_logon"> ${hibernate.connection.internal_logon}</prop> -->
<prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
<prop key="hibernate.show_sql"> ${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto"> ${hibernate.hbm2ddl_auto}</prop>
<!-- <prop key="hibernate.default_schema"> ${hibernate.default_schema}</prop> -->
</props>
</property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
and now
package org.azizkhani.dao;
import java.util.List;
import org.azizkhani.core.QueryResult;
public interface IGenericRepository<T> {
public List<T> getAll();
public List<T> getAll(String where, String order, int pageNumber,int pageSize);
public QueryResult<T> getAllGrid(String where, String order, int pageNumber,int pageSize);
public T loadByEntityId(int entityId);
public T single(String where);
public void add(T entity);
public void delete(T entity);
public void deleteByEntityId(int entityId);
public void update(T entity);
public int count();
}
implementation
package org.azizkhani.dao.impl.hibernate;
import java.util.List;
import org.azizkhani.common.utility.HQLUtility;
import org.azizkhani.core.QueryResult;
import org.azizkhani.dao.IGenericRepository;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public abstract class GenericRepository<T> implements IGenericRepository<T> {
protected Class<T> domainClass = getDomainClass();
protected abstract Class<T> getDomainClass();
@Autowired
private SessionFactory sessionFactory;
@Override
public List<T> getAll() {
Criteria criteria = getSession().createCriteria(domainClass.getName());
return criteria.list();
}
public Session getSession() {
try {
return sessionFactory.getCurrentSession();
} catch (Exception e) {
System.out.println(e.getMessage().toString());
}
return sessionFactory.openSession();
}
public QueryResult<T> getAllGrid(String where, String order, int pageNumber, int pageSize) {
Session session = getSession();
StringBuffer jql = new StringBuffer("from " + domainClass.getName()
+ " e ");
HQLUtility.toHQL(jql, where, order);
Query query = session.createQuery(jql.toString());
if (pageSize > 0)
{
query.setFirstResult(pageNumber * pageSize);
query.setMaxResults(pageSize);
}
List<T> list = query.list();
StringBuffer jqlCount = new StringBuffer("select count(*) from "
+ domainClass.getName() + " e ");
HQLUtility.toHQL(jqlCount, where, "");
int count = ((Long) getSession().createQuery(jqlCount.toString())
.uniqueResult()).intValue();
return new QueryResult<T>(pageNumber, count, pageSize, list);
}
@Override
public List<T> getAll(String where, String order, int pageNumber, int pageSize) {
Session session = getSession();
StringBuffer jql = new StringBuffer("from " + domainClass.getName()
+ " e ");
HQLUtility.toHQL(jql, where, order);
Query query = session.createQuery(jql.toString());
if (pageSize > 0)
{
query.setFirstResult(pageNumber * pageSize);
query.setMaxResults(pageSize);
}
return query.list();
}
@Transactional(readOnly=true)
@Override
public T loadByEntityId(int entityId) {
return (T) getSession().load(domainClass, entityId);
}
@Override
public T single(String where) {
return null;
}
@Override
public void add(T entity) {
Session session = getSession();
session.beginTransaction();
session.save(entity);
session.getTransaction().commit();
session.close();
}
@Override
public void delete(T entity) {
getSession().delete(entity);
}
@Override
public void deleteByEntityId(int entityId) {
Session session = getSession();
session.beginTransaction();
Object obj = session.load(domainClass, entityId);
session.delete(obj);
session.getTransaction().commit();
session.close();
}
@Override
public void update(T entity) {
Session session = getSession();
session.beginTransaction();
session.update(entity);
session.getTransaction().commit();
session.close();
}
@Override
public int count() {
int count = ((Long) getSession().createQuery(
"select count(*) from " + domainClass.getName()).uniqueResult())
.intValue();
return count;
}
}