Meu arquivo Web.xml esta assim:
<?xml version="1.0"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>ExampleProject Web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>#{skinBean.skin}</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
<param-value>com.sun.facelets.FaceletViewHandler</param-value>
</context-param>
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<listener>
<listener-class>
org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<!-- 30 MINUTOS -->
<session-timeout>30</session-timeout>
</session-config>
</web-app>
Meu applicationContext.xml esta assim:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="false">
<context:annotation-config />
<context:component-scan base-package="br.com.sansoftware">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<tx:annotation-driven proxy-target-class="true" />
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/oxrh" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="hibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
<prop key="hibernate.format_sql">false</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>
<bean id="PROPAGATION_REQUIRES"
class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="transactionManager"/>
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRES"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref local="myDataSource" />
</property>
<property name="hibernateProperties">
<ref bean="hibernateProperties" />
</property>
<property name="packagesToScan" value="br.com.sansoftware" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
</bean>
</beans>
Não tem mapeamento de bean no xml, ele e efetuado atraves destas linhas:
<property name="packagesToScan" value="br.com.sansoftware" />
E aqui o mapeamento do Dao
<context:component-scan base-package="br.com.sansoftware">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan>
E anotando seus Daos assim:
@Repository
public class EmployeeDaoImpl extends GenericDAOImpl<Employee, Long> implements EmployeeDao{
protected final static Log log = LogFactory.getLog(EmployeeDaoImpl.class);
protected Class<Employee> getEntityClass() {
return Employee.class;
}
}
Interface Dao generico
package br.com.sansoftware.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Criterion;
import br.com.sansoftware.model.Supplier;
public interface GenericDAO<T, ID extends Serializable>{
T findById(ID id);
T findByIdString(String id);
void persist(T entity);
void update(T entity);
void delete(T entity);
List<T> loadAll();
String[] filds(Class<?> classe);
void delete(ID id);
List<T> findAllByProperty(String propertyName, Object value);
List<T> findAllByPropertyLong(String propertyName, Object value);
List list();
}
Implementacao Dao Generico
package br.com.sansoftware.dao;
@Repository
public abstract class GenericDAOImpl<T, ID extends Serializable> extends HibernateDaoSupport implements GenericDAO<T, ID>{
private Class<T> persistentClass;
protected abstract Class<T> getEntityClass();
protected DetachedCriteria createDetachedCriteria() {
return DetachedCriteria.forClass(getEntityClass());
}
public Class<T> getPersistentClass() {
return this.persistentClass;
}
@SuppressWarnings("unchecked")
protected GenericDAOImpl(){
}
@Transactional(propagation = Propagation.REQUIRED)
public void delete(T entity) {
// TODO Auto-generated method stub
getHibernateTemplate().delete(entity);
}
public T findById(ID id) {
// TODO Auto-generated method stub
return getHibernateTemplate().get(getEntityClass(), id);
}
public T findByIdString(String id) {
// TODO Auto-generated method stub
return getHibernateTemplate().get(getEntityClass(), id);
}
@SuppressWarnings("unchecked")
public List<T> loadAll() {
return getHibernateTemplate().loadAll(getEntityClass());
}
public List list() {
return getHibernateTemplate().loadAll(getEntityClass());
}
@Transactional(propagation = Propagation.REQUIRED)
public void persist(T entity) {
getHibernateTemplate().persist(entity);
}
@Transactional(propagation = Propagation.REQUIRED)
public void update(T entity) {
getHibernateTemplate().merge(entity);
}
@Transactional(propagation = Propagation.REQUIRED)
public void delete(ID id) {
T entity = findById(id);
getHibernateTemplate().delete(entity);
}
public String[] filds(Class<?> classe) {
try {
Field fieldlist[] = classe.getDeclaredFields();
String[] fields = new String[fieldlist.length-1];
for (int i = 0; i <= fieldlist.length-1; i++) {
Field fld = fieldlist[i];
Annotation[] annotations = fld.getAnnotations();
for (Annotation a : annotations) {
for (Method m : a.annotationType().getDeclaredMethods()) {
if(a.annotationType()!=javax.persistence.Transient.class){
fields[i]=fld.getName();
}
}
}
}
return fields;
}
catch (Throwable e) {
System.err.println(e);
}
return null;
}
@SuppressWarnings("unchecked")
public List<T> findAllByProperty(String propertyName, Object value) {
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.like(propertyName, "%"+value+"%"));
return getHibernateTemplate().findByCriteria(criteria);
}
public List<T> findAllByPropertyLong(String propertyName, Object value){
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq(propertyName, value));
return getHibernateTemplate().findByCriteria(criteria);
}
}
Interface EmployeDao
package br.com.sansoftware.dao;
import br.com.sansoftware.model.Employee;
public interface EmployeeDao extends GenericDAO<Employee, Long>{
}