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.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void createTroop(Troop troop) throws IllegalArgumentException {

    if (troop == null || troop.getId() != null || troop.getName() == null || troop.getMission() == null
            || troop.getAmountOfMoney() == null) {
        throw new IllegalArgumentException("Create troop called with wrong param");
    }//from ww w.ja v  a2 s  .c o m
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(troop);
    em.getTransaction().commit();
    em.close();
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User findUser(int id) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    User user = (User) em.find(User.class, id);
    em.getTransaction().commit();// w w  w.  ja  va2 s. co m
    return user;
}

From source file:org.spc.ofp.tubs.domain.purseseine.TripRepository.java

public void save(final PurseSeineTrip trip) {
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    try {//from   w w w.ja  va 2s .  c  o m
        xa.begin();
        mgr.persist(trip);
        mgr.flush();
        xa.commit();
        mgr.refresh(trip);
    } catch (Exception ex) {
        if (xa.isActive()) {
            xa.rollback();
        }
    } finally {
        mgr.close();
    }
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User setApiKey(User user, String apiKey) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    User u = (User) em.find(User.class, user.getId());
    u.setApiKey(apiKey);//from   www.  ja  v  a2s  .  co  m
    em.getTransaction().commit();

    return u;
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User createUser(User user) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    String apiKey = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16).toUpperCase();
    user.setApiKey(apiKey);/*w w w.  j  a v a  2s . c  o  m*/
    em.persist(user);
    em.getTransaction().commit();

    return user;
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User addBookmark(User user, Bookmark bookmark) {
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    User u = findUser(user.getEmailAddress());
    bookmark.setUser(u);//from   w w  w . j  ava2  s  .  c  om
    em.persist(bookmark);
    u.getBookmarks().add(bookmark);
    em.getTransaction().commit();

    return u;
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User deleteBookmark(User user, String bookmarkId) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Bookmark bookmark = (Bookmark) em.find(Bookmark.class, bookmarkId);
    if (user.getId() != bookmark.getUser().getId()) {
        throw new RuntimeException("user ids don't match when deleting a bookmark");
    }/*from w  w w . j a  v  a 2s  .com*/
    user = (User) em.find(User.class, user.getId());
    user.getBookmarks().remove(bookmark);
    em.remove(bookmark);
    em.getTransaction().commit();

    return user;
}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void removeTroop(Troop troop) throws IllegalArgumentException {
    if (troop == null) {
        throw new IllegalArgumentException("Troop can't be null.");
    }//from  ww w  . ja  v  a2 s  .co m
    if (troop.getId() == null) {
        throw new IllegalArgumentException("Troop is not present in DB.");
    }
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Troop present = em.find(Troop.class, troop.getId());
    em.getTransaction().commit();

    if (present == null) {
        throw new IllegalArgumentException("Troop is not present in DB.");
    } else {
        em.getTransaction().begin();
        em.remove(em.contains(troop) ? troop : em.merge(troop));
        em.getTransaction().commit();
    }
    em.close();
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User addBookmarks(User user, List<Bookmark> bookmarks) {
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    User u = findUser(user.getEmailAddress());

    for (Bookmark bookmark : bookmarks) {
        bookmark.setUser(u);/*  w  w w  .  ja va2s .  co m*/
        em.persist(bookmark);
        u.getBookmarks().add(bookmark);
    }

    em.getTransaction().commit();

    return u;
}

From source file:com.ewcms.publication.PublishIntegratedTest.java

private void updateTemplateSourceStutas() {
    JpaTemplate template = createJpaTemplate();
    template.execute(new JpaCallback<Object>() {
        @Override/*from   w  w  w .  j a v a2s.c  om*/
        public Object doInJpa(EntityManager em) throws PersistenceException {
            em.getTransaction().begin();
            String hql = "Update TemplateSource o Set o.release= ?1 Where o.release = ?2";
            Query query = em.createQuery(hql);
            query.setParameter(1, Boolean.FALSE);
            query.setParameter(2, Boolean.TRUE);
            query.executeUpdate();
            em.getTransaction().commit();
            return null;
        }
    });
}