Example usage for javax.persistence EntityManager persist

List of usage examples for javax.persistence EntityManager persist

Introduction

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

Prototype

public void persist(Object entity);

Source Link

Document

Make an instance managed and persistent.

Usage

From source file:com.enioka.jqm.tools.Helpers.java

static RUser createUserIfMissing(EntityManager em, String login, String description, RRole... roles) {
    RUser res = null;// ww w  . j  a v  a 2 s. c o  m
    try {
        res = em.createQuery("SELECT r from RUser r WHERE r.login = :l", RUser.class).setParameter("l", login)
                .getSingleResult();
    } catch (NoResultException e) {
        res = new RUser();
        res.setFreeText(description);
        res.setLogin(login);
        res.setPassword(String.valueOf((new SecureRandom()).nextInt()));
        encodePassword(res);
        em.persist(res);
    }
    res.setLocked(false);
    for (RRole r : res.getRoles()) {
        r.getUsers().remove(res);
    }
    res.getRoles().clear();
    for (RRole r : roles) {
        res.getRoles().add(r);
        r.getUsers().add(res);
    }

    return res;
}

From source file:hd.controller.AddImageToProjectServlet.java

public void persist(Object object) {
    EntityManager em = emf.createEntityManager();
    try {//from   w ww . j  a  va 2s. c  o  m
        em.getTransaction().begin();
        em.persist(object);
        em.getTransaction().commit();
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
}

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

@SuppressWarnings("finally")
public boolean store(DBStorageEntry entry) throws Exception {

    if (update(entry)) {
        return true;
    }//from  w w  w  . j  av  a  2s.  co  m

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

    try {
        em.getTransaction().begin();
        em.persist(entry);
        em.getTransaction().commit();
    } catch (Exception e) {
        result = false;
        throw e;
    } finally {
        em.close();
        return result;
    }
}

From source file:org.spc.ofp.tubs.domain.common.CommonRepository.java

public boolean saveVessel(final Vessel vessel) {
    boolean success = false;
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    xa.begin();//  w ww.  j  a v a 2 s.c  o  m
    try {
        mgr.persist(vessel);
        xa.commit();
        success = true;
    } catch (Exception ignoreMe) {
        rollbackQuietly(xa);
    } finally {
        mgr.close();
    }
    return success;
}

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();/*from  w w w  .ja v a 2 s  . c o  m*/
    try {
        entityManager.persist(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

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

/**
 * Register a new infrastructure.//w w w  .  j  av  a2 s  . co m
 *
 * @param infra The infrastructure to register
 * @return The registered infrastructure
 */
@POST
@Status(Response.Status.CREATED)
@Consumes({ MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE })
@Produces(Constants.INDIGOMIMETYPE)
public final Infrastructure createInfrastructure(final Infrastructure infra) {
    Date now = new Date();
    infra.setDateCreated(now);
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        em.persist(infra);
        et.commit();
        log.debug("New infrastructure registered: " + infra.getId());
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to create the infrastructure");
        log.error(re);
    } finally {
        em.close();
    }
    return infra;
}

From source file:org.viafirma.persistencia.dao.GenericEjb3Dao.java

/**
 * Persiste la entidad indicada//w  w  w  .  j  a v a 2  s .  c om
 * @throws ExcepcionNoCreado 
 */
public T create(T entidad) throws ExcepcionNoCreado {
    EntityManager manager = getEntityManager();
    try {
        manager.getTransaction().begin();
        log.config("Creando " + entidad);
        manager.persist(entidad);
        manager.getTransaction().commit();
        return entidad;
    } catch (PersistenceException e) {
        manager.getTransaction().rollback();
        log.severe("Error al crear entidad" + e.getMessage());
        throw new ExcepcionNoCreado(e.getMessage());
    } finally {
        manager.close();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java

@Override
public boolean create(User user) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {//  w ww .  j av  a 2  s.co  m
        em.getTransaction().begin();
        em.persist(user);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.CheckDocumentRepositoryImplementation.java

@Override
public boolean create(CheckDocument something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {/* ww  w  . j  a v a  2 s  .c o m*/
        em.getTransaction().begin();
        em.persist(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:com.chiralbehaviors.CoRE.access.resource.CollectionResource.java

@POST
@Path("{parentId}")
@Produces(MediaType.APPLICATION_JSON)/*from  www.  jav a 2s  .com*/
public Product createNewProduct(@PathParam("parentId") String parentId, @QueryParam("relId") String relId,
        Product child) throws JsonProcessingException {
    return perform(new Transactionally<Product>() {

        @Override
        public Product exec(Model model) throws SQLException {
            EntityManager em = model.getEntityManager();
            Product parent = em.find(Product.class, parentId);
            Relationship rel = em.find(Relationship.class, relId);
            em.persist(child);
            ProductNetwork net = new ProductNetwork(parent, rel, child, parent.getUpdatedBy());
            em.persist(net);
            em.flush();
            em.refresh(child);
            return child;
        }
    });
}