E ae cara blz?
eu dei uma travada nesse capitulo ai tambem
porem meu problema naum era de codigo e sim de Jar´s errados
tinha que dar uma olhada no erro pra saber o que esta dando errado.
Segue abaixo a linha de codigos.
HibernateUtil
[code]package br.com.caelum.lojavirtual.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
static{
Configuration conf = new AnnotationConfiguration();
conf.configure();
factory = conf.buildSessionFactory();
}
public static Session getSession() {
return factory.openSession();
}
}[/code]
DaoFactory
[code]package br.com.caelum.lojavirtual.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.caelum.lojavirtual.modelo.Cd;
import br.com.caelum.lojavirtual.modelo.Musica;
import br.com.caelum.lojavirtual.util.HibernateUtil;
public class DaoFactory {
private final Session session;
private Transaction transaction;
public DaoFactory(){
session = HibernateUtil.getSession();
}
public void beginTransaction(){
this.transaction = this.session.beginTransaction();
}
public void commit(){
this.transaction.commit();
this.transaction = null;
}
public boolean hasTransaction(){
return this.transaction != null;
}
public void rollback(){
this.transaction.rollback();
this.transaction = null;
}
public void close(){
this.session.close();
}
public UsuarioDao getUsuarioDao(){
return new UsuarioDao(this.session);
}
public Dao<Cd> getCdDao(){
return new Dao<Cd>(this.session, Cd.class);
}
public Dao<Musica> getMusicaDao(){
return new Dao<Musica>(this.session, Musica.class);
}
}[/code]
Dao
[code]package br.com.caelum.lojavirtual.dao;
import java.util.List;
import org.hibernate.Session;
public class Dao {
private final Session session;
private final Class classe;
public Dao(Session session, Class classe){
this.session = session;
this.classe = classe;
}
public void adiciona(T u){
this.session.save(u);
}
public void remove(T u){
this.session.delete(u);
}
public void atualiza(T u){
this.session.merge(u);
}
public List<T> listaTudo(){
return this.session.createCriteria(this.classe).list();
}
public T procura(Long id){
return (T) session.load(this.classe, id);
}
protected Session getSession(){
return session;
}
}[/code]
DaoInterceptor
[code]package br.com.caelum.lojavirtual.logic;
import org.vraptor.Interceptor;
import org.vraptor.LogicException;
import org.vraptor.LogicFlow;
import org.vraptor.annotations.Out;
import org.vraptor.view.ViewException;
import br.com.caelum.lojavirtual.dao.DaoFactory;
public class DaoInterceptor implements Interceptor{
private DaoFactory factory = new DaoFactory();
public void intercept (LogicFlow flow) throws LogicException, ViewException{
//executa a logica
flow.execute();
//se sobrou transação sem comitar, faz um rollback
if(factory.hasTransaction()){
factory.rollback();
}
factory.close();
}
@Out(key="br.com.caelum.lojavirtual.dao.DaoFactory")
public DaoFactory getFactory(){
return factory;
}
}
[/code]
UsuarioLogic
[code]package br.com.caelum.lojavirtual.logic;
import java.util.List;
import org.vraptor.annotations.Component;
import org.vraptor.annotations.InterceptedBy;
import br.com.caelum.lojavirtual.dao.DaoFactory;
import br.com.caelum.lojavirtual.modelo.Usuario;
@Component
@InterceptedBy({AutorizadorInterceptor.class, DaoInterceptor.class})
public class UsuarioLogic {
private final DaoFactory daoFactory;
private List<Usuario> usuarios;
public UsuarioLogic (DaoFactory daoFactory){
this.daoFactory = daoFactory;
}
public void formulario(){
}
public void adiciona (Usuario usuario){
this.daoFactory.beginTransaction();
this.daoFactory.getUsuarioDao().adiciona(usuario);
this.daoFactory.commit();
}
public void lista(){
usuarios = this.daoFactory.getUsuarioDao().listaTudo();
}
public List<Usuario> getUsuarios(){
return usuarios;
}
public void remove(Usuario usuario){
this.daoFactory.beginTransaction();
this.daoFactory.getUsuarioDao().remove(usuario);
this.daoFactory.commit();
}
}[/code]