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.anthonychaves.bookmarks.service.TokenService.java

public String setupNewLoginToken(User user) {
    PersistentLoginToken token = new PersistentLoginToken();
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    User u = em.find(User.class, user.getId());
    token.setUser(u);/*from w w w. j  a v  a 2  s.  c o m*/
    em.persist(token);
    em.getTransaction().commit();

    return token.getId();
}

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

public User loginWithToken(String tokenId) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    PersistentLoginToken token = (PersistentLoginToken) em.find(PersistentLoginToken.class, tokenId);

    User user = null;/*  www.  j  av  a2s  .  co  m*/
    if (token != null) {
        user = token.getUser();
        em.remove(token);
        em.getTransaction().commit();
    } else {
        em.getTransaction().rollback();
        //TODO this is a forgery attempt
        throw new RuntimeException("Attempted login token cookie forgery");
    }
    return user;
}

From source file:streaming.service.DataBaseService.java

public void supprimerTout() {
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    em.getTransaction().begin();
    em.createQuery("DELETE FROM Lien l").executeUpdate();
    em.createQuery("DELETE FROM Episode e").executeUpdate();
    em.createQuery("DELETE FROM Saison s").executeUpdate();
    em.createQuery("DELETE FROM Serie se").executeUpdate();
    em.createQuery("DELETE FROM Film f").executeUpdate();
    em.createQuery("DELETE FROM Realisateur r").executeUpdate();
    em.createQuery("DELETE FROM Pays p").executeUpdate();
    em.createQuery("DELETE FROM Genre g").executeUpdate();
    em.getTransaction().commit();// ww  w  .j a  v a 2 s .  c o m
}

From source file:streaming.service.DBService.java

public void deleteAll() {
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    em.getTransaction().begin();
    em.createQuery("DELETE FROM Lien l").executeUpdate();
    em.createQuery("DELETE FROM Episode e").executeUpdate();
    em.createQuery("DELETE FROM Saison saison").executeUpdate();
    em.createQuery("DELETE FROM Serie serie").executeUpdate();
    em.createQuery("DELETE FROM Film f").executeUpdate();
    em.createQuery("DELETE FROM Genre g").executeUpdate();
    em.createQuery("DELETE FROM Realisateur r").executeUpdate();
    em.createQuery("DELETE FROM Pays p").executeUpdate();
    em.getTransaction().commit();//from w  w w .j a v  a2s  .  c  o  m
}

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

public Object[] updateTags(User user, int id, String tags) {
    String cleanTags = cleanTags(tags);

    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    User u = em.find(User.class, user.getId());
    Bookmark b = em.find(Bookmark.class, id);
    if (b.getUser().getId() != u.getId()) {
        throw new RuntimeException("Please don't try to delete bookmarks that aren't yours.");
    }//from  w w w .j a v  a  2  s. c  om

    String originalTags = b.getTags();
    b.setTags(cleanTags);
    em.getTransaction().commit();

    List<String> diffTags = diffTags(originalTags, cleanTags);

    return new Object[] { u, b, diffTags };
}

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

public List<BookmarkDetail> findBookmarksByTag(String tag, User user) {
    List<byte[]> bookmarkIds = new ArrayList<byte[]>();
    try {/*from  w ww . j a v  a  2  s. c  o m*/
        bookmarkIds = jredis.lrange(tag, 0, 50000);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    User u = em.find(User.class, user.getId());
    javax.persistence.Query query = em.createQuery(
            "select new net.anthonychaves.bookmarks.models.BookmarkDetail(b.id, b.title, b.url, b.tags) from Bookmark b where b.id in (:ids) and b.user = :user order by b.id desc")
            .setParameter("ids", bookmarkIds).setParameter("user", u);
    List<BookmarkDetail> bookmarks = (List<BookmarkDetail>) query.getResultList();
    em.getTransaction().rollback();

    return bookmarks;
}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void create(Task task) throws SQLException {

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();
    manager.persist(task);//from w ww . j  av  a2  s .  c  o  m
    manager.getTransaction().commit();
    manager.close();

}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void update(Task task) throws SQLException {

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();
    manager.merge(task);//from   w  ww  . ja  v  a  2s .c  o m
    manager.getTransaction().commit();
    manager.close();

}

From source file:bq.jpa.demo.basic2.dao.AccountDaoImpl.java

/**
 * inject entitymanagerfactory, can support bean-transaction
 *//*  w  w w . j a  va  2  s . co  m*/
@Override
public Account addWithBeanTransaction(Account account) {
    EntityManager localEM = emf.createEntityManager();
    localEM.getTransaction().begin();
    localEM.persist(account);
    localEM.getTransaction().commit();
    localEM.close();
    return account;
}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void delete(Long id) throws SQLException {

    Task task = this.read(id);

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();
    task = manager.merge(task);//  w w w. ja v  a2  s  .  co m
    manager.remove(task);
    manager.getTransaction().commit();
    manager.close();

}