I know what you have mentioned about business and DAO layer, but this is my first example with Spring + Hibernate and I only wanted to send a request form a browser that response a list of data from the database, using a Spring Controller that invokes directly the DAO that retrieves data using Hibernate.
I cannot understand what you want to say in your sentence:
I take it you access the "eventoTx" bean and not "eventoDAO"?
My bean and transaction configuration have been "inspired" in the petClinic sample.
Code:
<bean id="clinicTarget" class="org.springframework.samples.petclinic.hibernate.HibernateClinic">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="clinic" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="clinicTarget"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
Respect to my configuration and code, in my web.xml I use the ContextLoaderListener to load some config application context, one per layer and a DispatchServlet to implement the MVC pattern.
Code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/pruebas-webflows.xml, /WEB-INF/pruebas-service.xml,/WEB-INF/pruebas-data-hibernate.xml, /WEB-INF/pruebas-security.xml</param-value>
</context-param>
...
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
<servlet>
<servlet-name>pruebas-control</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pruebas-control</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
This is the pruebas-control-servlet.xml file:
Code:
<beans>
<bean id="eventoController" class="com.sqs.pruebas.control.web.event.EventoController">
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/evento.htm">
<ref bean="eventoController" />
</entry>
</map>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
This is the class EventoController which instantiate the DAO (should be the service class):
Code:
package com.sqs.pruebas.control.web.event;
import com.sqs.pruebas.domain.event.Evento;
import com.sqs.pruebas.model.dao.event.EventoDAOImpl;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class EventoController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
PropertyConfigurator.configure(NotificacionController.class.getResource("/config/log4j.properties"));
Log log = LogFactory.getLog(NotificacionController.class);
log.debug("....................................");
EventoDAOImpl evDAO = new EventoDAOImpl();
Collection eventos = evDAO.loadEventosById("01");
for(Iterator it=eventos.iterator(); it.hasNext(); ) {
log.debug(((Evento)it.next()).toString());
}
log.debug("....................................");
return new ModelAndView("/listaEventos");
}
}
listaEventos.jsp only iterate over the result list of events obtain with the DAO (really only one event).
The rest of the classes and XML files is in previous post.
Thanks,
Antonio.