Opensessioninviewfilter + HibernateTransactionManager

3 respostas
M

Galera, estou tentando implementar o pradrão Opensessioninviewfilter mais n estou conseguindo persistir os dados! faço a chamada mais ele não da o commit na session. gostaria de saber o que estou fazendo de errado.. segue o código. OBG pela ajuda!!

Minha configuração do Spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="no" default-dependency-check="none"
	default-lazy-init="true">

	<context:annotation-config />

	<context:component-scan
		base-package="br.com.baseProjectFilter.negocios.controladores"
		annotation-config="true" />
	<context:component-scan base-package="br.com.baseProjectFilter.negocios.fachada"
		annotation-config="true" />
	<context:component-scan base-package="br.com.baseProjectFilter.repositorio.DAO"
		annotation-config="true" />


	<!-- Hibernate SessionFactory -->

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.postgresql.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:postgresql://localhost/base
			</value>
		</property>
		<property name="username">
			<value>postgres</value>
		</property>
		<property name="password">
			<value>123</value>
		</property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>Usuario.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.c3p0.minPoolSize">3</prop>
				<prop key="hibernate.c3p0.maxPoolSize">20</prop>
				<prop key="hibernate.c3p0.max_statement">200</prop>
			</props>
		</property>
	</bean>


	<!-- transactionManager -->

	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="inserir" propagation="REQUIRED" />
			<tx:method name="remover" propagation="REQUIRED" />
			<tx:method name="editar" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="service"
			expression="execution (*
    br.com.baseProjectFilter.repositorio.DAO.imp.*.* (..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
	</aop:config>


</beans>
Meu web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
	version="2.4">

	<display-name>baseProjectFilter</display-name>

	<!--
		There are three means to configure Wickets configuration mode and they
		are tested in the order given. 1) A system property:
		-Dwicket.configuration 2) servlet specific <init-param> 3) context
		specific <context-param> The value might be either "development"
		(reloading when templates change) or "deployment". If no configuration
		is found, "development" is the default.
	-->

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>


	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>
			org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>sessionFactory</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>wicket.baseProjectFilter</filter-name>
		<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
		<init-param>
			<param-name>applicationClassName</param-name>
			<param-value>br.com.baseProjectFilter.WicketApplication</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>wicket.baseProjectFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


</web-app>
meu dao do hibernate!
package br.com.baseProjectFilter.repositorio.infra;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import br.com.baseProjectFilter.negocios.entidades.Entidade;
import br.com.baseProjectFilter.repositorio.filtro.IFiltro;


public abstract class HibernateGenericDAO<ENTIDADE extends Entidade, ID extends Serializable, FILTRO extends IFiltro<ENTIDADE>>
implements IGenericDAO<ENTIDADE, ID, FILTRO> {

	private Class<ENTIDADE> classe;

	@Autowired
	private SessionFactory sessionFactory;

	public HibernateGenericDAO(Class<ENTIDADE> classe) {
		this.classe = classe;
	}

	@SuppressWarnings("unchecked")
	public ENTIDADE buscar(final ID id) {
		return (ENTIDADE) this.getSession().get(classe, id);
	}

	public void editar(final ENTIDADE obj) {
		this.getSession().update(obj);
	}

	public List<ENTIDADE> filtrar(final FILTRO filtro) {
		// TODO Implementar HibernateGenericDAO -> filtrar
		return null;
	}


	public Class<ENTIDADE> getClasse() {
		return classe;
	}

	public Session getSession() {
		return SessionFactoryUtils.getSession(sessionFactory, false);
	}

	public void inserir(final ENTIDADE obj) {
		this.getSession().save(obj);
	}

	@SuppressWarnings("unchecked")
	public List<ENTIDADE> listar() {
		return this.getSession().createQuery("FROM " + classe.getName()).list();
	}

	public void remover(final ENTIDADE obj) {
		this.getSession().delete(obj);
	}

	public void setClasse(final Class<ENTIDADE> classe) {
		this.classe = classe;
	}

}

estou quebrando a cabeça e n acho solução! agradeço a ajuda galera!!

3 Respostas

M

não sei se tem alguma coisa haver mais estou usando jetty e wicket… eu preciso de alguma configuração differenciada para usar essas tecnologias ??

P

Acho que vc ta perguntando em lugar errado, e melhor vc postar a sua msg na área sobre persistencia.

M

blz… postei la!! mais vc sabe o problema :slight_smile:

Criado 21 de março de 2011
Ultima resposta 24 de mar. de 2011
Respostas 3
Participantes 2