GenericDAO
Code:
package com.mycompany.commons.data.dao.support;
import java.io.Serializable;
import java.util.List;
public interface GenericDAO<T, Id extends Serializable> {
public T findById(Id id, boolean lock);
public List<T> findAll();
public void save(T t);
public void update(T t);
public void saveOrUpdate(T t);
public void deleteById(Id id);
public void deleteAll();
public Integer count();
}
GenericDAOHibernateImpl
Code:
package com.mycompany.commons.data.dao.support;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.springframework.transaction.annotation.Transactional;
//Must put this here or get "No Hibernate Session bound to thread"
@Transactional
public abstract class GenericDAOHibernateImpl<T, Id extends Serializable> implements GenericDAO<T, Id>{
private Class<T> persistentClass;
//IoC properties
private SessionFactory sf;
@SuppressWarnings("unchecked")
public GenericDAOHibernateImpl() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public T findById(Id id, boolean lock) {
T result;
if (lock) {
result = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
}
else {
result = (T) getSession().load(getPersistentClass(), id);
}
return result;
}
public List<T> findAll() {
return findByCriteria();
}
public void save(T t) {
getSession().save(t);
}
public void update(T t) {
getSession().update(t);
}
public void saveOrUpdate(T t) {
getSession().saveOrUpdate(t);
}
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
Criteria crit = getSession().createCriteria(getPersistentClass());
for (Criterion c : criterion) {
crit.add(c);
}
return crit.list();
}
public void deleteById(Id id) {
T t = findById(id, false);
getSession().delete(t);
}
public void deleteAll() {
getSession().createQuery("delete").executeUpdate();
}
public Integer count() {
return (Integer) getSession().createQuery(
"select count(*) from " + getPersistentClass().getName()).uniqueResult();
}
public Class<T> getPersistentClass() {
return persistentClass;
}
protected Session getSession() {
return sf.getCurrentSession();
}
//IoC setters
public void setSessionFactory(SessionFactory sf) {
this.sf = sf;
}
}
SiteDAO
Code:
package com.mycompany.commons.data.dao;
import com.mycompany.commons.data.dao.support.GenericDAO;
import com.mycompany.domain.Site;
public interface SiteDAO extends GenericDAO<Site, Long> {
//Nothing to add at the moment
}
SiteDAOGenericHibernateImpl
Code:
package com.healthdec.commons.data.dao.impl;
import com.mycompany.commons.data.dao.SiteDAO;
import com.mycompany.commons.data.dao.support.GenericDAOHibernateImpl;
import com.mycompany.domain.Site;
public class SiteDAOGenericHibernateImpl extends GenericDAOHibernateImpl<Site, Long> implements SiteDAO {
//Nothing to add at the moment
}
applicationContext.xml
Code:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<!-- Transaction behavior based on annotations -->
<tx:annotation-driven/>
<!-- Data source -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@database.mycompany.com1521:DEVELOPMENT"/>
<property name="username" value="devuser"/>
<property name="password" value="password"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="500"/>
<property name="validationQuery" value="SELECT SYSDATE FROM DUAL"/>
</bean>
<!-- Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>com.mycompany.domain.Site</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<!-- Transaction -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"/>
<!-- DAOs -->
<bean id="siteDAO" class="com.healthdec.mycompany.data.dao.impl.SiteDAOGenericHibernateImpl"/>
</beans>