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:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteAllProducts() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/* w ww. j a v a2s  .  com*/

    Query q = entityManager.createQuery("DELETE FROM Product ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteAllUsers() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from   w  ww .j ava2 s  .c  o m*/

    Query q = entityManager.createQuery("DELETE FROM FStoreUser ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public Product updateProduct(Product bm) {
    logger.info("================= updateProduct ==================");
    logger.info("bmgetId=" + bm.getId());
    logger.info("bm getName= " + bm.getName());
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from  w  w  w. ja va 2 s .  c o m
    Product resis = entityManager.merge(bm);
    entityTransaction.commit();

    return resis;
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteAllCategories() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//w w w.  j a  v  a2  s  .com

    Query q = entityManager.createQuery("DELETE FROM Category");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public UserSession saveUserSession(UserSession userSession) {
    logger.info("Will userSession = " + userSession.getUsername());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*from  w w  w  .jav  a2  s .c o  m*/

    FStoreUser u = entityManager.find(FStoreUser.class, userSession.getUser().getId());
    userSession.setUser(u);

    logger.info("Will userSession = " + u.toString());

    entityManager.persist(u);
    entityManager.persist(userSession);
    entityManager.flush();
    entityTransaction.commit();

    return userSession;

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void saveUser(FStoreUser bu) {
    logger.info("Will save FStoreUser = " + bu.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from  ww w  . jav a 2 s .c o  m*/

    entityManager.persist(bu);
    //      List<ApplicationMetadata> apps = bu.getApps();
    //      for (ApplicationMetadata app : apps) {         
    //         entityManager.persist(app.getCategory());
    //         entityManager.persist(app);
    //      }
    //      
    //      List<BunMetadata> buns = bu.getBuns() ;
    //      for (BunMetadata bun : buns) {         
    //         entityManager.persist(bun.getCategory());
    //         entityManager.persist(bun);
    //      }

    entityManager.flush();
    entityTransaction.commit();

}

From source file:nl.b3p.viewer.admin.stripes.ConfigureSolrActionBean.java

public Resolution removeFromIndex() {
    EntityManager em = Stripersist.getEntityManager();
    SolrServer server = SolrInitializer.getServerInstance();
    solrConfiguration = em.find(SolrConf.class, solrConfiguration.getId());
    SolrUpdateJob.removeSolrConfigurationFromIndex(solrConfiguration, em, server);
    em.getTransaction().commit();
    return new ForwardResolution(EDIT_JSP);

}

From source file:info.dolezel.jarss.rest.v1.FeedsService.java

@DELETE
@Path("{id}")
public Response unsubscribeFeed(@Context SecurityContext context, @PathParam("id") int feedId) {
    EntityManager em;
    EntityTransaction tx;// ww w  .ja  v  a  2s . c o m
    User user;
    Feed f;
    FeedData fd;

    em = HibernateUtil.getEntityManager();
    tx = em.getTransaction();
    user = (User) context.getUserPrincipal();

    try {
        tx.begin();

        try {
            f = (Feed) em.createQuery("from Feed where id = :id", Feed.class).setParameter("id", feedId)
                    .getSingleResult();
        } catch (NoResultException e) {
            return Response.status(Response.Status.NOT_FOUND)
                    .entity(new ErrorDescription("Feed does not exist")).build();
        }
        if (!f.getUser().equals(user)) {
            return Response.status(Response.Status.FORBIDDEN)
                    .entity(new ErrorDescription("Feed not owned by user")).build();
        }

        fd = f.getData();
        em.remove(f);

        if (fd.getFeeds().isEmpty())
            em.remove(fd);

        tx.commit();

        return Response.noContent().build();
    } finally {
        if (tx.isActive())
            tx.rollback();
        em.close();
    }
}

From source file:nl.b3p.viewer.admin.stripes.ConfigureSolrActionBean.java

@WaitPage(path = "/WEB-INF/jsp/waitpage.jsp", delay = 2000, refresh = 1000, ajax = "/WEB-INF/jsp/waitpageajax.jsp")
public Resolution addToIndex() throws InterruptedException {
    removeFromIndex();//  w  ww .  j a  v  a2  s .c om
    status = new WaitPageStatus();
    EntityManager em = Stripersist.getEntityManager();
    SolrServer server = SolrInitializer.getServerInstance();
    solrConfiguration = em.find(SolrConf.class, solrConfiguration.getId());
    SolrUpdateJob.insertSolrConfigurationIntoIndex(solrConfiguration, em, status, server);
    em.getTransaction().commit();
    return new ForwardResolution(EDIT_JSP);
}

From source file:de.zib.gndms.infra.system.GNDMSystemDirectory.java

/**
 * Removes old configlets from the {@link #configlets} map.
 *
 * Checks for every <tt>Configlet</tt> in the map, if still exists in the database.
 * If not, the map entry will be removed and <tt>shutdown()</tt> invoked on the old configlet entry. 
 *
 * @param emParam an EntityManager managing <tt>Configlet</tt>s
 *//*w  ww.j av  a  2s. co m*/
@SuppressWarnings({ "SuspiciousMethodCalls" })
private synchronized void shutdownOldConfiglets(final EntityManager emParam) {
    Set<String> set = configlets.keySet();
    Object[] keys = set.toArray();
    for (Object name : keys) {
        emParam.getTransaction().begin();
        try {
            if (emParam.find(ConfigletState.class, name) == null) {
                Configlet let = configlets.get(name);
                configlets.remove(name);
                let.shutdown();
            }
        } catch (RuntimeException e) {
            logger.warn(e);
        } finally {
            if (emParam.getTransaction().isActive())
                emParam.getTransaction().commit();
        }
    }
}