Example usage for javax.persistence EntityTransaction begin

List of usage examples for javax.persistence EntityTransaction begin

Introduction

In this page you can find the example usage for javax.persistence EntityTransaction begin.

Prototype

public void begin();

Source Link

Document

Start a resource transaction.

Usage

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void delete(PK pk) {
    Validate.notNull(pk, "Null pk parameter");
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    getEntityManager().remove(find(pk));
    tx.commit();//from w  w w  .  ja v  a  2s  .  co m
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void delete(T entity) {
    Validate.notNull(entity, "Null Entity parameter");
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    getEntityManager().remove(entity);/*  w w w  .j a  va2s.  c  o m*/
    tx.commit();
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void insert(Collection<T> entities) {
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    for (T entity : entities) {
        getEntityManager().persist(entity);
    }/*  w w w.  ja  v  a2 s .  c  o m*/
    tx.commit();
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void update(Collection<T> entities) {
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    for (T entity : entities) {
        getEntityManager().merge(entity);
    }//ww  w  .  j av a 2s. co  m
    tx.commit();
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void delete(Collection<T> entities) {
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    for (T entity : entities) {
        getEntityManager().remove(entity);
    }/* w ww  .  ja v  a  2s. co m*/
    tx.commit();
}

From source file:org.apache.openjpa.persistence.event.TestBeforeCommit.java

public void testQuery() {
    OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) emf.createEntityManager();
    em.addTransactionListener(this);
    EntityTransaction tran = em.getTransaction();

    tran.begin();
    ae = doQuery(em);/*  www .  j av a2s  . c  o  m*/
    if (dict instanceof OracleDictionary) {
        assertNull(ae.getName());
    } else if (dict instanceof SybaseDictionary) {
        // Sybase converts empty strings to " " 
        assertEquals(" ", ae.getName());
    } else {
        assertEquals("", ae.getName());
    }
    assertEquals(1, ae.getVersion());
    tran.commit();

    ae = doQuery(em);
    assertEquals("Ava", ae.getName());
    assertEquals(2, ae.getVersion());

    em.clear();
    ae = em.find(AnEntity.class, PKID);
    assertEquals("Ava", ae.getName());
    assertEquals(2, ae.getVersion());

    tran.begin();
    tran.commit();
    em.clear();
    ae = em.find(AnEntity.class, PKID);
    assertEquals("AvaAva", ae.getName());
    assertEquals(3, ae.getVersion());

    em.close();
}

From source file:com.sdl.odata.datasource.jpa.JPADataSource.java

@Override
public void delete(ODataUri uri, EntityDataModel entityDataModel) throws ODataException {
    Option<Object> entity = extractEntityWithKeys(uri, entityDataModel);

    if (entity.isDefined()) {
        Object jpaEntity = entityMapper.convertODataEntityToDS(entity.get(), entityDataModel);
        if (jpaEntity != null) {
            EntityManager entityManager = getEntityManager();
            EntityTransaction transaction = entityManager.getTransaction();
            try {
                transaction.begin();

                Object attached = entityManager.merge(jpaEntity);
                entityManager.remove(attached);
            } catch (PersistenceException e) {
                LOG.error("Could not remove entity: {}", entity);
                throw new ODataDataSourceException("Could not remove entity", e);
            } finally {
                if (transaction.isActive()) {
                    transaction.commit();
                } else {
                    transaction.rollback();
                }//w  w  w .j  ava  2  s .  c o m
            }
        } else {
            throw new ODataDataSourceException("Could not remove entity, could not be loaded");
        }
    }
}

From source file:facades.PersonFacadeDB.java

@Override
public Person delete(Integer id) throws NotFoundException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    Person p = em.find(Person.class, id);

    if (p == null) {
        transaction.rollback();/*from  w ww.  ja v a 2  s .  co m*/
        throw new NotFoundException("No person for the given id");
    } else {
        em.remove(p);
        transaction.commit();
    }

    return p;
}

From source file:org.eclipse.jubula.client.core.persistence.Persistor.java

/**
 * /*from   w w w . j a va 2s.  com*/
 * @param em
 *            entity manager
 * @throws PersistenceException
 *             if we blow up
 */
private static void createOrUpdateDBGuard(EntityManager em) throws PersistenceException {
    EntityTransaction tx = null;
    try {
        tx = em.getTransaction();
        tx.begin();

        try {
            em.createQuery("select guard from DbGuardPO as guard").getSingleResult(); //$NON-NLS-1$
        } catch (NoResultException nre) {
            LockManager.initDbGuard(em);
        }

        tx.commit();
    } catch (PersistenceException pe) {
        if (tx != null) {
            tx.rollback();
        }
        throw pe;
    }
}

From source file:facades.PersonFacadeDB.java

@Override
public Person addPerson(String json) {
    //make person from Json
    Person p = gson.fromJson(json, Person.class);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    try {//from ww w  . j  a  v  a  2  s . c  om
        em.persist(p);
        transaction.commit();
    } catch (Exception e) {
        transaction.rollback();
    } finally {
        em.close();
    }
    return p;
}