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

public void saveCategory(Category c) {
    logger.info("Will category = " + c.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//  ww  w . ja  va2s  .c om

    entityManager.persist(c);
    entityManager.flush();
    entityTransaction.commit();
}

From source file:org.sigmah.server.servlet.exporter.models.ProjectReportModelHandler.java

@Override
public void importModel(InputStream inputStream, EntityManager em, User user) throws Exception {
    ObjectInputStream objectInputStream;
    em.getTransaction().begin();/*from   www .  j  a v a 2s  . c o  m*/
    try {
        objectInputStream = new ObjectInputStream(inputStream);
        ProjectReportModel projectReportModel = (ProjectReportModel) objectInputStream.readObject();

        projectReportModel.resetImport(modelesReset, modelesImport);
        saveProjectReportModelElement(projectReportModel, em);
        projectReportModel.setOrganization(user.getOrganization());
        em.persist(projectReportModel);
        em.getTransaction().commit();
    } catch (IOException e) {
        LOG.debug(e);
        throw e;
    }
}

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

public void saveProperty(FStoreProperty p) {
    logger.info("Will FStoreProperty = " + p.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/* w  ww  .j  av  a2  s. c o m*/

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

}

From source file:com.gigglinggnus.controllers.ManageExamsController.java

/**
 *
 * @param request servlet request//  w ww.  j a va  2s.  c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EntityManager em = (EntityManager) request.getSession().getAttribute("em");

    String examId = request.getParameter("examid");
    String status = request.getParameter("status");
    Clock clk = (Clock) (request.getSession().getAttribute("clock"));
    User user = (User) (request.getSession().getAttribute("user"));

    Exam exam = em.find(Exam.class, examId);

    try {
        em.getTransaction().begin();
    } catch (Exception e) {
    }

    try {
        if (status.equals("approved")) {
            user.changeExamStatus(exam, ExamStatus.APPROVED);
            em.persist(exam);
            em.getTransaction().commit();
            request.setAttribute("msg", "Exam APPROVED");
        } else if (status.equals("denied")) {
            user.changeExamStatus(exam, ExamStatus.DENIED);
            em.persist(exam);
            em.getTransaction().commit();
            request.setAttribute("msg", "Exam Denied");
        } else {
            request.setAttribute("msg", "Invalid status");
        }
    } catch (Exception e) {
        em.getTransaction().rollback();
        request.setAttribute("msg", e.toString());
        throw e;
    }

    RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
    rd.forward(request, response);
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.SessionDaoImpl.java

@Override
@Transactional// w ww  . j  ava  2s.  c om
public void clearSessionUserList(long sessionId, boolean isChairList) {
    final SessionImpl sessionImpl = this.getSession(sessionId);
    final EntityManager entityManager = this.getEntityManager();

    if (isChairList) {//clear chair list
        for (final ConferenceUserImpl user : sessionImpl.getChairs()) {
            user.getChairedSessions().remove(sessionImpl);
            entityManager.persist(user);
        }
        sessionImpl.getChairs().clear();
        entityManager.persist(sessionImpl);
    } else { //clear non chair list
        for (final ConferenceUserImpl user : sessionImpl.getNonChairs()) {
            user.getNonChairedSessions().remove(sessionImpl);
            entityManager.persist(user);
        }
        sessionImpl.getNonChairs().clear();
        entityManager.persist(sessionImpl);
    }
}

From source file:org.batoo.jpa.benchmark.BenchmarkTest.java

private void doBenchmarkPersist(final EntityManagerFactory emf, Person[][] allPersons) {
    for (final Person[] persons : allPersons) {
        for (final Person person : persons) {
            final EntityManager em = this.open(emf);

            em.persist(person);

            this.close(em);
        }/*w  ww.j  av a2  s  .  c  o  m*/
    }
}

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();/*w  w w  .  j  ava  2 s  .c  om*/

    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:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testReadingMultipleTimesWithASingleConnection() throws IOException {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//  w w  w  .j  av  a2 s  . c om
    LobEntity le = (LobEntity) em.find(getLobEntityClass(), 1);
    String string = createLobData2();
    changeStream(le, string);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    le = (LobEntity) em.find(getLobEntityClass(), 1);
    assertNotNull(le.getStream());
    LobEntity entity = newLobEntity(createLobData(), 2);
    em.persist(entity);
    assertEquals(string, getStreamContentAsString(le.getStream()));
    em.getTransaction().commit();
    em.close();
}

From source file:com.impetus.client.couchdb.crud.CouchDBClientTest.java

@Test
@PerfTest(invocations = 10)/*from   www .  ja v a2s  . co m*/
public void testCRUDWithBatch() {
    Map<String, String> batchProperty = new HashMap<String, String>(1);
    batchProperty.put(PersistenceProperties.KUNDERA_BATCH_SIZE, "5");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(_PU, batchProperty);
    EntityManager em = emf.createEntityManager();
    Map<String, Client> clients = (Map<String, Client>) em.getDelegate();
    CouchDBClient client = (CouchDBClient) clients.get(_PU);
    Assert.assertEquals(5, ((Batcher) client).getBatchSize());

    final String originalName = "vivek";

    for (int i = 0; i < 9; i++) {
        PersonCouchDB object = new PersonCouchDB();
        object.setAge(32);
        object.setPersonId(ROW_KEY + i);
        object.setPersonName(originalName);
        em.persist(object);

        if (i >= 5) {
            PersonCouchDB result = (PersonCouchDB) client.find(PersonCouchDB.class, ROW_KEY + i);
            Assert.assertNull(result);
        } else if (i > 0 && i % 4 == 0) {
            PersonCouchDB result = (PersonCouchDB) client.find(PersonCouchDB.class, ROW_KEY + i);
            Assert.assertNotNull(result);
            Assert.assertEquals(result.getPersonId(), object.getPersonId());
            Assert.assertEquals(result.getAge(), object.getAge());
            Assert.assertEquals(result.getPersonName(), object.getPersonName());
        }
    }
    em.flush();
    em.clear();
    em.close();
    em = null;
}

From source file:com.haulmont.cuba.core.QueryResultTest.java

@Test
public void test() {
    Transaction tx;/*www. ja v  a2  s.co  m*/
    javax.persistence.EntityManager emDelegate;
    EntityManager em;
    Query query;

    UUID sessionId = UUID.randomUUID();
    int queryKey = 1;

    tx = cont.persistence().createTransaction();
    try {
        emDelegate = cont.persistence().getEntityManager().getDelegate();

        QueryResult queryResult = new QueryResult();
        queryResult.setSessionId(sessionId);
        queryResult.setQueryKey(queryKey);
        queryResult.setEntityId(UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));

        emDelegate.persist(queryResult);

        tx.commitRetaining();

        em = cont.persistence().getEntityManager();
        query = em.createQuery("select u from sec$User u, sys$QueryResult qr "
                + "where qr.entityId = u.id and qr.sessionId = ?1 and qr.queryKey = ?2");
        query.setParameter(1, sessionId);
        query.setParameter(2, queryKey);
        query.setView(new View(User.class).addProperty("login").addProperty("name").addProperty("group",
                new View(Group.class).addProperty("name")));

        //            OpenJPAQuery openJPAQuery = (OpenJPAQuery) query.getDelegate();
        //            Map params = new HashMap();
        //            params.put(1, sessionId);
        //            params.put(2, queryKey);
        //            String[] dataStoreActions = openJPAQuery.getDataStoreActions(params);
        //
        //            System.out.println(dataStoreActions);

        List<User> list = query.getResultList();
        assertEquals(1, list.size());
        User user = list.get(0);
        assertEquals("admin", user.getLogin());

        tx.commit();
    } finally {
        tx.end();
    }
}