97 lines
2.1 KiB
Plaintext
Executable File
97 lines
2.1 KiB
Plaintext
Executable File
package com.fp.sessionbeans.ejb;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.ejb.Stateless;
|
|
import javax.ejb.TransactionManagement;
|
|
import javax.ejb.TransactionManagementType;
|
|
import javax.persistence.EntityManager;
|
|
import javax.persistence.EntityTransaction;
|
|
|
|
import com.fp.dto.hb.HibernateBean;
|
|
import com.fp.sessionbeans.ejb.interfaces.CrudBeanLocal;
|
|
|
|
/**
|
|
* Session Bean utilitario utilizada en insert de registros a la base.
|
|
*
|
|
* @author Jorge VAca.
|
|
* @version 2.1
|
|
*/
|
|
@Stateless
|
|
@TransactionManagement(value = TransactionManagementType.BEAN)
|
|
public class CrudBean implements CrudBeanLocal {
|
|
|
|
/** Version de la Clase */
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
/**
|
|
* Metodo que entrega de grbar un registrp en la base haciendo commit.
|
|
*
|
|
* @param em
|
|
* Referencia a una session de base de datos.
|
|
* @param pBean
|
|
* Objeto a almacenar en la base de datos.
|
|
* @throws Exception
|
|
*/
|
|
public void insert(EntityManager em, HibernateBean pBean) throws Exception {
|
|
EntityTransaction tx = em.getTransaction();
|
|
tx.begin();
|
|
try {
|
|
em.persist(pBean);
|
|
tx.commit();
|
|
} catch (Exception e) {
|
|
if (tx.isActive()) {
|
|
tx.rollback();
|
|
}
|
|
throw e;
|
|
} finally {
|
|
try {
|
|
if (em.isOpen()) {
|
|
em.clear();
|
|
em.close();
|
|
}
|
|
} catch (Exception e) {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Metodo que entrega de grbar un registrp en la base haciendo commit.
|
|
*
|
|
* @param em
|
|
* Referencia a una session de base de datos.
|
|
* @param pLbean
|
|
* Lista con objetos a almacenar en la base de datos.
|
|
* @throws Exception
|
|
*/
|
|
public void insert(EntityManager em, List<HibernateBean> pLbean)
|
|
throws Exception {
|
|
EntityTransaction tx = em.getTransaction();
|
|
tx.begin();
|
|
try {
|
|
for (HibernateBean obj : pLbean) {
|
|
em.persist(obj);
|
|
}
|
|
tx.commit();
|
|
} catch (Exception e) {
|
|
if (tx.isActive()) {
|
|
tx.rollback();
|
|
}
|
|
throw e;
|
|
} finally {
|
|
try {
|
|
if (em.isOpen()) {
|
|
em.clear();
|
|
em.close();
|
|
}
|
|
} catch (Exception e) {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|