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:fr.mby.opa.picsimpl.dao.DbProposalDao.java

public AbstractUnitProposal createProposal(final AbstractUnitProposal proposal) {
    Assert.notNull(proposal, "No Proposal supplied !");
    Assert.isNull(proposal.getId(), "Id should not be set for creation !");

    new TxCallback(this.getEmf()) {

        @Override//w  w  w.jav  a2  s.  c o  m
        protected void executeInTransaction(final EntityManager em) {
            em.persist(proposal);
        }
    };

    return proposal;
}

From source file:twity.TwityServlet.java

private void persistUser(String guid, OAuthConsumer consumer) {
    EntityManager em = EMF.get().createEntityManager();
    User u = new User(guid, consumer.getToken(), consumer.getTokenSecret());
    try {/*from   w  ww. ja  v  a2  s  .co m*/
        em.persist(u);
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("unable to persist user: " + guid);
    } finally {
        em.close();
    }
}

From source file:fr.mby.opa.picsimpl.dao.DbProposalDao.java

@Override
public ProposalBranch createBranch(final ProposalBranch branch) {
    Assert.notNull(branch, "No ProposalBag supplied !");
    Assert.isNull(branch.getId(), "Id should not be set for creation !");

    new TxCallback(this.getEmf()) {

        @Override// w  w  w .j a v  a 2s .  c  om
        protected void executeInTransaction(final EntityManager em) {
            em.persist(branch);
        }
    };

    return branch;
}

From source file:edu.csueb.cs6320.utils.UserService.java

public boolean createUser(User user, String newPassword) {

    String salt = Auth.getSalt();
    user.setSalt(salt);//from   w w  w  . j a va2s . c  om
    try {
        user.setSaltedHashedPassword(Auth.hashPassword(newPassword, salt));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return false;
    }

    EntityManager em = Persistence.createEntityManagerFactory("TestPU").createEntityManager();
    em.getTransaction().begin();
    em.persist(user);
    em.getTransaction().commit();
    return true;
}

From source file:com.sdl.odata.datasource.jpa.JPADataSource.java

@Override
public Object create(ODataUri uri, Object entity, EntityDataModel entityDataModel) throws ODataException {
    Object jpaEntity = entityMapper.convertODataEntityToDS(entity, entityDataModel);
    EntityManager entityManager = getEntityManager();
    EntityTransaction transaction = entityManager.getTransaction();
    try {/*w w  w  . ja v  a2 s. co m*/
        transaction.begin();

        LOG.info("Persisting entity: {}", jpaEntity);
        entityManager.persist(jpaEntity);

        return entityMapper.convertDSEntityToOData(jpaEntity, entity.getClass(), entityDataModel);
    } finally {
        if (transaction.isActive()) {
            transaction.commit();
        } else {
            transaction.rollback();
        }
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public void putMonitoredFeed(String feedName) {

    MonitoredFeedsBean monitoredFeedsBean = new MonitoredFeedsBean();
    monitoredFeedsBean.setFeedName(feedName);
    EntityManager entityManager = getEntityManager();
    try {/*from  www  .  j  a  v a 2s . c o m*/
        beginTransaction(entityManager);
        entityManager.persist(monitoredFeedsBean);
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:net.sf.ehcache.openjpa.datacache.TestEhCache.java

@Test
public void testPersist() {
    EntityManagerFactory emf = em.getEntityManagerFactory();

    EntityManager em = emf.createEntityManager();
    PObject pc = new PObject("XYZ");
    em.getTransaction().begin();/* w ww.j  a  va2  s.com*/
    em.persist(pc);
    em.getTransaction().commit();
    Object oid = pc.getId();

    em.clear();
    // After clean the instance must not be in L1 cache
    assertFalse(em.contains(pc));
    // But it must be found in L2 cache by its OpenJPA identifier
    assertTrue(getCache(pc.getClass()).contains(getOpenJPAId(pc, oid)));

    PObject pc2 = em.find(PObject.class, oid);
    // After find(), the original instance is not in the L1 cache
    assertFalse(em.contains(pc));
    // After find(), the found instance is in the L1 cache
    assertTrue(em.contains(pc2));
    // The L2 cache must still hold the key   
    assertTrue(getCache(pc.getClass()).contains(getOpenJPAId(pc, oid)));
}

From source file:org.rhq.enterprise.server.drift.DriftServerTest.java

private void initDB() throws Exception {
    purgeDB();//from www .  ja v  a2  s  .co m
    executeInTransaction(new TransactionCallback() {
        @Override
        public void execute() throws Exception {
            EntityManager em = getEntityManager();
            initResourceType();
            initAgent();
            initResource();

            em.persist(resourceType);
            em.persist(agent);
            resource.setAgent(agent);
            em.persist(resource);

            initDB(em);
        }
    });
}

From source file:de.zib.gndms.infra.action.SlicedTaskFlowAction.java

protected void updateSlice(Slice slice) {
    final EntityManager em = getEntityManager();
    final TxFrame txf = new TxFrame(em);
    try {/*www  . j ava 2  s .co m*/
        em.persist(slice);
        txf.commit();
    } finally {
        txf.finish();
    }
}

From source file:com.edugility.substantia.substance.TestCasePerson.java

@Test
public void testingJPA() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    assertNotNull(emf);//www .j ava 2  s.  c o m

    final EntityManager em = emf.createEntityManager();
    assertNotNull(em);

    final EntityTransaction et = em.getTransaction();
    assertNotNull(et);
    et.begin();

    final Person p = new Person();
    em.persist(p);
    em.flush();
    assertFalse(p.isTransient());
    assertTrue(p.isVersioned());
    assertEquals(Long.valueOf(1L), p.getId());
    assertEquals(Integer.valueOf(1), p.getVersion());

    et.commit();
    et.begin();

    final Person p2 = em.find(Person.class, 1L, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    assertNotNull(p2);
    assertFalse(p2.isTransient());
    assertTrue(p2.isVersioned());
    assertEquals(Long.valueOf(1L), p2.getId());
    assertEquals(Integer.valueOf(1), p2.getVersion());

    et.commit();
    et.begin();

    final Person p3 = em.getReference(Person.class, 1L);
    assertNotNull(p3);
    assertFalse(p3.isTransient());
    assertTrue(p3.isVersioned());
    assertEquals(Long.valueOf(1L), p3.getId());
    assertEquals(Integer.valueOf(2), p3.getVersion());

    et.commit();
    et.begin();

    assertTrue(em.contains(p));
    em.remove(p);

    et.commit();

    em.close();

    emf.close();
}