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:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java

@Test
public void testCreateTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    troopC.setMission("mise C");
    entityManager.getTransaction().commit();
    entityManager.close();/*from ww w.  ja va 2 s  .  com*/

    troopDao.createTroop(troopC);

    Troop t1 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t1.getName(), "Testers");
    Assert.assertEquals(t1.getMoney().compareTo(new BigDecimal("30")), 0);
    Assert.assertEquals(t1.getMission(), "mise C");

}

From source file:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java

@Test
public void testDeleteTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    troopC.setMission("mise C");
    entityManager.persist(troopC);//from www .  j  a v  a 2  s.  c  om
    entityManager.getTransaction().commit();
    entityManager.close();

    Troop t1 = troopDao.findTroopById(troopC.getId());
    troopDao.deleteTroop(t1);

    Troop t2 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t2, null);

}

From source file:com.doculibre.constellio.wicket.session.ConstellioSession.java

/**
 * Can't override Session#setLocale(Locale) classe Session.
 *///ww w  . jav a  2 s .c  om
public void changeLocale(Locale locale) {
    ConstellioUser user = (ConstellioUser) getUser();
    if (user == null) {
        setLocale(locale);
    } else {
        EntityManager entityManager = ConstellioPersistenceContext.getCurrentEntityManager();
        if (!entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().begin();
        }
        user.setLocale(locale);
        entityManager.persist(user);
        entityManager.getTransaction().commit();
    }
}

From source file:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java

@Test
public void testAssignMissionToTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    entityManager.persist(troopC);//w  ww . j a  va  2 s . c o  m
    entityManager.getTransaction().commit();
    entityManager.close();

    troopDao.assignMissionToTroop(troopC.getId(), "mise C");

    Troop t1 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t1.getMission(), "mise C");

    troopDao.assignMissionToTroop(troopC.getId(), null);

    Troop t2 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t2.getMission(), null);

}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskCollectionService.java

/**
 * Retrieve a task list for the user.//from www .  java 2s.  c om
 * Tasks are retrieved from the storage for the user performing the request.
 *
 * @return A list of tasks
 */
private List<Task> retrieveTaskList() {
    List<Task> lstTasks = new LinkedList<>();
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    List<Object[]> taskList = null;
    try {
        et = em.getTransaction();
        et.begin();
        taskList = em.createNamedQuery("tasks.userAll").setParameter("user", getUser()).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the task list");
        log.error(re);
        throw new RuntimeException("Impossible to access the task list");
    } finally {
        em.close();
    }
    if (taskList != null && !taskList.isEmpty()) {
        for (Object[] elem : taskList) {
            int idElem = 0;
            Task tmpTask = new Task();
            tmpTask.setId((String) elem[idElem++]);
            tmpTask.setDescription((String) elem[idElem++]);
            tmpTask.setStatus((Task.STATUS) elem[idElem++]);
            tmpTask.setDateCreated((Date) elem[idElem]);
            lstTasks.add(tmpTask);
        }
    }
    return lstTasks;
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreCreatedEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();//w w  w  . j a  v  a  2 s .com

    LivreEntry entry = new LivreEntry();
    entry.setEditeur(e.getEditeur());
    entry.setFormat(e.getFormat());
    entry.setImagePath(e.getImagePath());
    entry.setIsbn(e.getIsbn());
    entry.setLangue(e.getLangue());
    entry.setNbPage(e.getNbPage());
    entry.setResume(e.getResume());
    entry.setTitre(e.getTitre());
    entry.setTitreOriginal(e.getTitreOriginal());
    entry.setLu(e.isLu());
    entry.setPossede(e.isPossede());
    try {
        entry.setImageAsBase64(this.getImageAsBase64(e.getImagePath()));
    } catch (IOException ioe) {
        LivreListener.LOGGER.warn("Cannot save the thumbnail in database: ", ioe);
        entry.setImageAsBase64(null);
    }

    if (e.getSagaId() != null && !e.getSagaId().trim().isEmpty()) {
        SagaEntry saga = em.getReference(SagaEntry.class, e.getSagaId());
        entry.setSaga(saga);
    }

    em.persist(entry);

    t.commit();
}

From source file:edu.csueb.cs6320.utils.UserService.java

public List<User> getUserList() {
    EntityManager em = Persistence.createEntityManagerFactory("TestPU").createEntityManager();

    em.getTransaction().begin();
    List<User> users = em.createQuery("SELECT u FROM User u", User.class).getResultList();
    em.getTransaction().commit();/*  w w w.  ja  v  a 2  s .com*/
    em.close();

    return users;
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreUpdatedEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();/*from  w w w.  j  ava 2s . c o  m*/

    LivreEntry entry = em.find(LivreEntry.class, e.getIsbn());
    entry.setEditeur(e.getEditeur());
    entry.setFormat(e.getFormat());
    entry.setImagePath(e.getImagePath());
    entry.setIsbn(e.getIsbn());
    entry.setLangue(e.getLangue());
    entry.setNbPage(e.getNbPage());
    entry.setResume(e.getResume());
    entry.setTitre(e.getTitre());
    entry.setTitreOriginal(e.getTitreOriginal());
    entry.setLu(e.isLu());
    entry.setPossede(e.isPossede());
    try {
        entry.setImageAsBase64(this.getImageAsBase64(e.getImagePath()));
    } catch (IOException ioe) {
        LivreListener.LOGGER.warn("Cannot save the thumbnail in database: ", ioe);
        entry.setImageAsBase64(null);
    }

    if (e.getImagePath() == null || e.getImagePath().isEmpty()) {
        entry.setImageAsBase64(null);
    }

    if (e.getSagaId() != null && !e.getSagaId().trim().isEmpty()) {
        SagaEntry saga = em.getReference(SagaEntry.class, e.getSagaId());
        entry.setSaga(saga);
    } else {
        entry.setSaga(null);
    }

    t.commit();
}

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

/**
 * Get the EntityManager associated with the current thread of control.
 * @param isTransactionRequired true if a transaction is begun if not
 * already active// w w  w  .  j a va2s . co  m
 * @return the EntityManager
 */
EntityManager getEntityManager(boolean isTransactionRequired) {
    EntityManager em = getThreadLocalEntityManager();
    if (isTransactionRequired && !em.getTransaction().isActive()) {
        em.getTransaction().begin();
    }
    return em;
}

From source file:edu.csueb.cs6320.utils.UserService.java

public boolean createUser(User user, String newPassword) {

    String salt = Auth.getSalt();
    user.setSalt(salt);//  w w w  .  j  a v  a 2s  .  co  m
    try {
        user.setSaltedHashedPassword(Auth.hashPassword(newPassword, salt));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return false;
    }

    EntityManager em = Persistence.createEntityManagerFactory("TestPU").createEntityManager();
    em.getTransaction().begin();
    em.persist(user);
    em.getTransaction().commit();
    return true;
}