Example usage for javax.persistence EntityManager getTransaction

List of usage examples for javax.persistence EntityManager getTransaction

Introduction

In this page you can find the example usage for javax.persistence EntityManager getTransaction.

Prototype

public EntityTransaction getTransaction();

Source Link

Document

Return the resource-level EntityTransaction object.

Usage

From source file:net.sf.ehcache.openjpa.datacache.TestEhCache.java

@Test
public void testPersist() {
    EntityManagerFactory emf = em.getEntityManagerFactory();

    EntityManager em = emf.createEntityManager();
    PObject pc = new PObject("XYZ");
    em.getTransaction().begin();
    em.persist(pc);/*  www.java  2  s  .c  o  m*/
    em.getTransaction().commit();
    Object oid = pc.getId();

    em.clear();
    // After clean the instance must not be in L1 cache
    assertFalse(em.contains(pc));
    // But it must be found in L2 cache by its OpenJPA identifier
    assertTrue(getCache(pc.getClass()).contains(getOpenJPAId(pc, oid)));

    PObject pc2 = em.find(PObject.class, oid);
    // After find(), the original instance is not in the L1 cache
    assertFalse(em.contains(pc));
    // After find(), the found instance is in the L1 cache
    assertTrue(em.contains(pc2));
    // The L2 cache must still hold the key   
    assertTrue(getCache(pc.getClass()).contains(getOpenJPAId(pc, oid)));
}

From source file:com.sun.socialsite.business.impl.JPAPersistenceStrategy.java

/**
 * Release database session, rolls back any uncommitted changes.
 *///from   w w  w.  j  a va 2s .  c o  m
public void release() {
    EntityManager em = getEntityManager(false);
    if (isTransactionActive(em)) {
        em.getTransaction().rollback();
    }
    em.close();
    setThreadLocalEntityManager(null);
}

From source file:com.sun.socialsite.business.impl.JPAPersistenceStrategy.java

/**
 * Return true if a transaction is active on the current EntityManager.
 * @param em the persistence manager/*  w  w w. j a v a  2 s .c  o  m*/
 * @return true if the persistence manager is not null and has an active
 *         transaction
 */
private boolean isTransactionActive(EntityManager em) {
    if (em == null) {
        return false;
    }
    return em.getTransaction().isActive();
}

From source file:cz.fi.muni.pa165.dto.BookDAOTest.java

@Test
public void testDelete() {
    EntityManager em = emf.createEntityManager();
    BookDAOImpl bdao = new BookDAOImpl();
    bdao.setManager(em);/*from w  ww  .  j  a v a2s. c  o  m*/
    Book b = new Book();
    b.setIdBook(1);
    em.getTransaction().begin();
    b = bdao.find(b);
    bdao.delete(b);
    em.getTransaction().commit();

    List<Book> books = em.createQuery("SELECT b FROM Book b", Book.class).getResultList();
    em.close();
    assertEquals(books.size(), 0);
}

From source file:com.edsoft.teknosaproject.bean.ReportBean.java

@Override
public void documentListener() {
    Menus menus;/* w w  w  . j  av  a2  s  .  com*/
    documentMenu.clear();
    path = Paths.get(DIR, "AnaDepo", family, type, brand);
    //path = Paths.get("D:", "Teknosa", family, type, brand);
    fileList = path.toFile().listFiles();
    EntityManager em = Persistence.createEntityManagerFactory("teknosa").createEntityManager();
    em.getTransaction().begin();
    Query query = em.createNamedQuery("Menus.findByValued");
    documentMenu.add(new SelectItem("", "Seiniz"));
    for (File file : fileList) {
        if (file.isDirectory()) {
            query.setParameter("valued", file.getName());
            menus = (Menus) query.getResultList().get(0);
            menus.setLabel(menus.getLabel1());
            menus.setValue(menus.getValued());
            documentMenu.add(menus);
        }
    }
    em.getTransaction().commit();
    em.close();
}

From source file:fr.xebia.demo.wicket.blog.service.GenericService.java

public void save(T entity) throws ServiceException {
    try {/*from  w  ww.ja  v a2 s .  c o m*/
        EntityManager entityManager = currentEntityManager();
        entityManager.getTransaction().begin();

        entityManager.persist(entity);

        commitTransaction();
    } catch (PersistenceException e) {
        logger.error(e.getCause(), e);
        rollbackTransaction();
        throw new ServiceException("Can't update object", e);
    } finally {
        closeEntityManager();
    }
}

From source file:com.edsoft.teknosaproject.bean.ReportBean.java

@Override
public void brandListener() {
    Menus menus;/*from  ww  w .j a va 2s .c  o  m*/
    brandMenu.clear();
    documentMenu.clear();
    path = Paths.get(DIR, "AnaDepo", family, type);
    //path = Paths.get("D:", "Teknosa", family, type);
    fileList = path.toFile().listFiles();
    EntityManager em = Persistence.createEntityManagerFactory("teknosa").createEntityManager();
    em.getTransaction().begin();
    Query query = em.createNamedQuery("Menus.findByValued");
    brandMenu.add(new SelectItem("", "Seiniz"));
    for (File file : fileList) {
        if (file.isDirectory()) {
            query.setParameter("valued", file.getName());
            menus = (Menus) query.getResultList().get(0);
            menus.setLabel(menus.getLabel1());
            menus.setValue(menus.getValued());
            brandMenu.add(menus);
        }
    }
    em.getTransaction().commit();
    em.close();
}

From source file:fr.xebia.demo.wicket.blog.service.GenericService.java

public T update(T entity) throws ServiceException {
    try {/*from w  ww  .  ja v a 2 s . c o  m*/
        EntityManager entityManager = currentEntityManager();
        entityManager.getTransaction().begin();
        T loadedObject = entityManager.find(getObjectClass(), getObjectId(entity));
        T mergedEntity = merge(loadedObject, entity);
        T updatedEntity = entityManager.merge(mergedEntity);
        commitTransaction();
        return updatedEntity;
    } catch (PersistenceException e) {
        logger.error(e.getCause(), e);
        rollbackTransaction();
        throw new ServiceException("Can't update object", e);
    } finally {
        closeEntityManager();
    }
}

From source file:eu.optimis.trustedinstance.DBStorage.java

@SuppressWarnings("finally")
public boolean delete(String resourceKey) throws Exception {

    boolean result = true;
    EntityManager em = emf.createEntityManager();

    try {/*from  w w w . jav a  2 s . c om*/
        String jpql = "delete from DBStorageEntry se where se.key=\"" + resourceKey + "\"";
        em.getTransaction().begin();
        Query query = em.createQuery(jpql);
        int row = query.executeUpdate();
        em.getTransaction().commit();
        if (row == 0) {
            result = false;
        }
    } catch (Exception e) {
        result = false;
        throw e;
    } finally {
        em.close();
        return result;
    }
}

From source file:fr.xebia.demo.wicket.blog.service.GenericService.java

public void deleteById(Serializable id) throws ServiceException {
    try {/*from   w  w w.j  ava2 s. c om*/
        EntityManager entityManager = currentEntityManager();
        entityManager.getTransaction().begin();
        Object loadedEntity = entityManager.find(getObjectClass(), id);
        if (loadedEntity == null) {
            throw new ServiceException("Entity referenced by id " + id + " does not exist");
        }
        entityManager.remove(loadedEntity);
        commitTransaction();
    } catch (PersistenceException e) {
        logger.error(e.getCause(), e);
        rollbackTransaction();
        throw new ServiceException("Can't delete object", e);
    } finally {
        closeEntityManager();
    }
}