[RESOLVIDO] VRaptor - Controle de transação

Boa noite!
Gostaria de saber qual é a melhor forma, atualmente, de controlar transações em uma aplicação com VRaptor e Hibernate.
Tenho pesquisado bastante sobre isto, e encontrei diversas abordagens, porém várias datam de um ou dois anos atrás, talvez já estejam defasadas.
Obrigada!

Pelo que estou vendo, se eu utilizar no web.xml

<context-param>
    <param-name>br.com.caelum.vraptor.packages</param-name>
    <param-value>br.com.caelum.vraptor.util.hibernate</param-value>
</context-param>

no DAO preciso apenas fazer

@Component
public class UsuarioDao {

	private final Session session;

	public UsuarioDao(Session session) {
		this.session = session;
	}

	public void criar(Usuario usuario) {
		session.save(usuario);
	}
}

que o VRaptor já controla as transações.
É isto mesmo?

exato. Desse jeito ele abre uma transação no começo da request e commita ao final. Se isso for o suficiente pra vc, fica bem simples =)

Por hora é o que eu preciso.
Obrigada! :smiley:

Reabrindo o tópico… Lucas, quando incluí

<context-param>
    	<param-name>br.com.caelum.vraptor.packages</param-name>
    	<param-value>
    		br.com.caelum.vraptor.util.hibernate
	</param-value>
</context-param>

no web.xml, passou a dar o erro abaixo ao iniciar minha aplicação:

HTTP Status 500 - Error creating bean with name 'homeController': Unsatisfied dependency expressed through constructor argument with index 0 of type [br.com.projeto.business.HomeBusiness]: : Error creating bean with name 'homeBusiness': Unsatisfied dependency expressed through constructor argument with index 0 of type [br.com.projeto.dao.FraseDao]: : Error creating bean with name 'fraseDao': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.Session]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fraseDao': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.Session]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeBusiness': Unsatisfied dependency expressed through constructor argument with index 0 of type [br.com.projeto.dao.FraseDao]: : Error creating bean with name 'fraseDao': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.Session]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fraseDao': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.Session]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator, br.com.caelum.vraptor.plugin.hibernate4.SessionFactoryCreator]

Você poderia me ajudar com este erro?
Obrigada!

No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2:

se vc tá usando o plugin do hibernate 4, remova o context-param do pacote do hibernate.

Hum… estou usando sim. Neste caso não precisa nem da configuração no web.xml, o VRaptor controla automaticamente as transações?

é que só o fato de você colocar o jar do plugin do hibernate 4 ele já registra um interceptor de transações.

Então, sim =)

Certo, entendi.

Programar com VRaptor fica fácil, mas entender certinho todas as configurações tem sido uma tarefa árdua…

Obrigada novamente Lucas!

Obrigado pelo feedback =)

Gerar documentação é sempre um problema. Você pode nos ajudar criando issues indicando aonde está faltando documentação:

Ou até mesmo escrevendo você mesma a documentação:

Obrigado pelo feedback =)

Gerar documentação é sempre um problema. Você pode nos ajudar criando issues indicando aonde está faltando documentação:

Ou até mesmo escrevendo você mesma a documentação: