Last active
January 14, 2016 13:20
-
-
Save carlosync/f523f3c95b882fe79480 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erp.jpa; | |
import java.io.Serializable; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.enterprise.context.RequestScoped; | |
import javax.enterprise.inject.Disposes; | |
import javax.enterprise.inject.Produces; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
@ApplicationScoped | |
public class EntityManagerProducer implements Serializable{ | |
private static final long serialVersionUID = 1L; | |
private EntityManagerFactory factory; | |
public EntityManagerProducer() { | |
this.factory = Persistence.createEntityManagerFactory("Erp-empresaPU"); | |
} | |
@Produces @RequestScoped | |
public EntityManager create() { | |
return factory.createEntityManager(); | |
} | |
public void close(@Disposes EntityManager manager) { | |
if(manager.isOpen()){ | |
manager.close(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erp.dao; | |
import com.erp.model.Produto; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.inject.Inject; | |
import javax.persistence.EntityManager; | |
public class ProdutoDAO implements Serializable{ | |
private static final long serialVersionUID = 1L; | |
@Inject | |
private EntityManager manager; | |
public Produto porId(Long id){ | |
return manager.find(Produto.class, id); | |
} | |
public void salvar(Produto produto) { | |
this.manager.merge(produto); | |
} | |
public List<Produto> listarTodas() { | |
return this.manager.createNamedQuery("Produto.listarTodos", Produto.class).getResultList(); | |
} | |
public void excluir(Produto produto) { | |
produto = porId(produto.getId()); | |
manager.remove(produto); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erp.service; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.inject.Inject; | |
import com.erp.dao.ProdutoDAO; | |
import com.erp.jpa.Transactional; | |
import com.erp.model.Produto; | |
import com.erp.util.NegocioException; | |
public class ProdutoService implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Inject | |
private ProdutoDAO produtoDAO; | |
public Produto porId(Long id) throws NegocioException { | |
return produtoDAO.porId(id); | |
} | |
@Transactional | |
public void salvar(Produto produto) throws NegocioException { | |
produtoDAO.salvar(produto); | |
} | |
@Transactional | |
public void excluir(Produto produto) { | |
produtoDAO.excluir(produto); | |
} | |
public List<Produto> listarTodas() { | |
return produtoDAO.listarTodas(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erp.jpa; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import javax.interceptor.InterceptorBinding; | |
@InterceptorBinding | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
public @interface Transactional { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erp.jpa; | |
import java.io.Serializable; | |
import javax.annotation.Priority; | |
import javax.inject.Inject; | |
import javax.interceptor.AroundInvoke; | |
import javax.interceptor.Interceptor; | |
import javax.interceptor.InvocationContext; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityTransaction; | |
@Interceptor | |
@Priority(Interceptor.Priority.APPLICATION) | |
@Transactional // Anotação da Classe que foi criada no projeto | |
public class TransactionInterceptor implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Inject | |
private EntityManager manager; | |
@AroundInvoke | |
public Object invoke(InvocationContext context) throws Exception { | |
EntityTransaction transaction = manager.getTransaction(); | |
boolean owner = false; | |
try { | |
if (!transaction.isActive()) { | |
// truque para fazer rollback no que já passou | |
// (senão, um futuro commit, confirmaria até mesmo operações sem transação) | |
transaction.begin(); | |
transaction.rollback(); | |
// agora sim inicia a transação | |
transaction.begin(); | |
owner = true; | |
} | |
return context.proceed(); | |
} catch (Exception e) { | |
if (transaction != null && owner) { | |
transaction.rollback(); | |
} | |
throw e; | |
} finally { | |
if (transaction != null && transaction.isActive() && owner) { | |
transaction.commit(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para usar com Tomcat