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:org.spring.data.gemfire.app.dao.provider.JpaUserDao.java

@Transactional
public User save(final User user) {
    EntityManager localEntityManager = prepare(this.entityManager);
    localEntityManager.persist(user);
    localEntityManager.flush();//from  ww  w  .  j  a v  a  2 s.co m
    return user;
}

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

static RRole createRoleIfMissing(EntityManager em, String roleName, String description, String... permissions) {
    try {/*from  w w w.  j a v a  2  s. c  o  m*/
        return em.createQuery("SELECT rr from RRole rr WHERE rr.name = :r", RRole.class)
                .setParameter("r", roleName).getSingleResult();
    } catch (NoResultException e) {
        RRole r = new RRole();
        r.setName(roleName);
        r.setDescription(description);
        em.persist(r);

        for (String s : permissions) {
            RPermission p = new RPermission();
            p.setName(s);
            p.setRole(r);
            em.persist(p);
            r.getPermissions().add(p);
        }
        return r;
    }
}

From source file:core.commonapp.server.data.CreateRequiredData.java

/**
 * store all the objects read in//from w ww .  j av a2 s  .c  o  m
 */
public void store(List dataObjects) {
    log.info("Creating required data, storing {0} objects.", dataObjects.size());
    for (Iterator iter = dataObjects.iterator(); iter.hasNext();) {
        DataObject object = (DataObject) iter.next();
        // save the data object
        try {
            EntityManager entityManager = (EntityManager) context.getBean(EntityManager.class);
            entityManager.persist(object);
            log.info("Data object saved: {0}", object);
        } catch (Exception e) {
            log.error("Failed to save required data object: {0}", object);
        }

    }
}

From source file:org.croodie.resource.UserServerResource.java

@Override
public Representation createUser(User user) {
    EntityManagerFactory emf = EMF.get();
    EntityManager em = emf.createEntityManager();
    try {/*from  www.j  a va 2 s .  c  o m*/
        em.persist(user);
    } catch (Exception ex) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Internal Server Error", ex);
    } finally {
        em.close();
    }
    return new JacksonRepresentation<User>(user);
}

From source file:com.romb.hashfon.helper.Helper.java

public void persistObject(EntityManager em, Object o) {
    em.getTransaction().begin();
    em.persist(o);
    em.getTransaction().commit();
}

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

public void create(Task task) throws SQLException {

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();/*from   w  ww.j a  v a 2s . co  m*/
    manager.persist(task);
    manager.getTransaction().commit();
    manager.close();

}

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

/**
 * inject entitymanagerfactory, can support bean-transaction
 *///w  ww  .j ava  2 s.  c o  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:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param gatewayResource// w ww  .  j a v a  2  s  . c  o  m
 * @param userResource
 */
public static WorkerResource addGatewayWorker(GatewayResource gatewayResource, UserResource userResource)
        throws RegistryException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        if (!isGatewayExist(gatewayResource.getGatewayName())) {
            gatewayResource.save();
        }
        if (!isUserExist(userResource.getUserName())) {
            userResource.save();
        }
        Gateway gateway = em.find(Gateway.class, gatewayResource.getGatewayName());
        Users user = em.find(Users.class, userResource.getUserName());
        Gateway_Worker gatewayWorker = new Gateway_Worker();
        gatewayWorker.setGateway(gateway);
        gatewayWorker.setUser(user);
        em.persist(gatewayWorker);
        em.getTransaction().commit();
        em.close();
        return (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}

From source file:com.gerenciaProyecto.DaoImple.ProductoDaoImpl.java

@Override
@Transactional/*from w  w w.j av  a  2  s.c o  m*/
public void crear(Producto producto) {
    EntityManager em = getEntityManager();
    try {
        //            producto.set

        em.persist(producto);
        em.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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");
    }//  w  ww  .  j a  va 2  s  . com
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(troop);
    em.getTransaction().commit();
    em.close();
}