Olá e utenho as seguintes duvidas achei prático o criador de DAOs do netbeans, mas gostaria de saber como implemento o open session in view no Vraptor 3.
Sem modificar esses DAOs?? Mas se for pra modificar como eu faria? Ou esse DAO já tá protegido contra os problemas do lazy?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.caelum.lojavirtual.dao;
import br.com.caelum.lojavirtual.dao.exceptions.NonexistentEntityException;
import br.com.caelum.lojavirtual.modelos.Produto;
import br.com.caelum.vraptor.ioc.Component;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
/**
*
* @author Marcos
*/
@Component
public class ProdutoJpaController {
public ProdutoJpaController() {
emf = Persistence.createEntityManagerFactory("default");
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Produto produto) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(produto);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Produto produto) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
produto = em.merge(produto);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Long id = produto.getId();
if (findProduto(id) == null) {
throw new NonexistentEntityException("The produto with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Long id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Produto produto;
try {
produto = em.getReference(Produto.class, id);
produto.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The produto with id " + id + " no longer exists.", enfe);
}
em.remove(produto);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Produto> findProdutoEntities() {
return findProdutoEntities(true, -1, -1);
}
public List<Produto> findProdutoEntities(int maxResults, int firstResult) {
return findProdutoEntities(false, maxResults, firstResult);
}
private List<Produto> findProdutoEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select object(o) from Produto as o");
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Produto findProduto(Long id) {
EntityManager em = getEntityManager();
try {
return em.find(Produto.class, id);
} finally {
em.close();
}
}
public int getProdutoCount() {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select count(o) from Produto as o");
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}