Erro: JSF2, CDI e JPA2 - javax.persistence.TransactionRequiredException

2 respostas
V

Pessoal estou desenvolvendo um projeto utilizando: JSF 2, CDI (Weld) e JPA2 (com Eclipselink e sem utilzar EJB).
O projeto está apresentando o erro javax.el.ELException: javax.persistence.TransactionRequiredException ao acionar o método persist do EntityManager, os métodos de pesquisa funcionam normalmente (por isso vou ocultar).

Segue abaixo o código e caso alguém tenha alguma dica eu agradeço, pois já procurei no Google e não consegui resolver

@ManagedBean(name = "perfilBean")
@RequestScoped
public class PerfilBean{
	
	@Inject
	private PerfilService perfilService;
	
	private Perfil perfil;
	
	public PerfilBean(){
		
	}

	public void salvar(){
		this.perfilService.save(perfil);
	}
	
	public Perfil getPerfil() {
		return perfil;
	}


	public void setPerfil(Perfil perfil) {
		this.perfil = perfil;
	}
}
public interface PerfilService extends GenericService<Perfil>{

}
@Default
public class PerfilServiceImpl extends GenericServiceImpl<Perfil> implements PerfilService{

	private PerfilDAO perfilDAO;
	
	
	@Inject
	public PerfilServiceImpl(PerfilDAO perfilDAO) {
		super(perfilDAO);
		this.perfilDAO = perfilDAO;
	}

	...	
}
public interface GenericService<T> {

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void save(T entity);
	
	...
}
public class GenericServiceImpl<T> implements GenericService<T> {

	private GenericDao<T> genericDao;
	
	
	public GenericServiceImpl(GenericDao<T> genericDao) {
		this.genericDao = genericDao;
	}
	
	@Override
	public void save(T entity){
		genericDao.save(entity);
	}
	
	...
}
public interface PerfilDAO extends GenericDao<Perfil>{
	...
}
@Any @Default
public class PerfilDAOImpl extends GenericDaoImpl<Perfil> implements PerfilDAO{

	
	@Inject
	public PerfilDAOImpl() {
		super(Perfil.class);
	}
}
public interface GenericDao<T> {
	
	public void save(T entity);
	...
}
public class GenericDaoImpl<T> implements GenericDao<T> {
	
	private final Class<T> persistentClass;
	
	@PersistenceContext(unitName="catalogDB")
	private EntityManager entityManager;
	
	public GenericDaoImpl(final Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

		
	public EntityManager getEntityManager() {
		return entityManager;
	}
	
	
	/**
	 * 
	 * @return
	 */
	public Class<T> getPersistentClass() {
		return persistentClass;
	}

	@Override
	public void save(final T entity) {
		entityManager.persist(entity);
	}
	
}

Segue o persistence.xml

Erro

2 Respostas

ivansalvadori

para persistir, necessita ter uma transação, que de maneira mais comum é realizada pelo ejb, que vc deixou de lado.
A aplicação está rodando em um servidor jee?

V

Eu estou utilizando o Glassfish-3.1.1, mas o objetivo é que aplicação funcione também no Tomcat

Criado 29 de março de 2012
Ultima resposta 29 de mar. de 2012
Respostas 2
Participantes 2