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:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default persist//from www  . ja  va2  s  .  c  om
 * 
 * @param objs
 *            objects to persist
 * @throws PersistException
 *             on errors (transaction is being rolled back)
 */
public void persist(Collection<T> objs) throws PersistException {
    if (objs == null || objs.isEmpty()) {
        return;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        for (T obj : objs) {
            em.persist(obj);
        }
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw new PersistException(e.getMessage(), e);
    }
}

From source file:com.headissue.pigeon.service.AdminSurveyServiceTest.java

EntityManager mergeSurvey(SurveyValue _value) throws Exception {
    EntityManager _manager = factory.createEntityManager();
    try {//  www  .  j a  va2  s  .  com
        _manager.getTransaction().begin();
        Survey s = _manager.find(Survey.class, survey.getId());
        adminSurveyService.mergeSurvey(s, _value, UPDATE_DATE, _manager);
        _manager.persist(s);
        _manager.getTransaction().commit();
        survey = s;
    } catch (Exception e) {
        if (_manager.getTransaction().isActive()) {
            _manager.getTransaction().rollback();
        }
        throw e;
    }
    return _manager;
}

From source file:org.apache.openjpa.persistence.event.TestBeforeCommit.java

@Override
public void setUp() throws Exception {
    if (emf == null) {
        emf = createEMF(AnEntity.class);
    }// ww  w  .  ja v  a2  s .  com
    dict = ((JDBCConfiguration) emf.getConfiguration()).getDBDictionaryInstance();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tran = em.getTransaction();

    tran.begin();
    em.createQuery("Delete from AnEntity").executeUpdate();
    tran.commit();

    tran.begin();
    ae = new AnEntity();
    ae.setId(PKID);
    ae.setName("");
    em.persist(ae);
    tran.commit();
    em.close();
}

From source file:com.chiralbehaviors.CoRE.attribute.Attribute.java

@Override
public void link(Relationship r, Attribute child, Agency updatedBy, Agency inverseSoftware, EntityManager em) {
    AttributeNetwork link = new AttributeNetwork(this, r, child, updatedBy);
    em.persist(link);
    AttributeNetwork inverse = new AttributeNetwork(child, r.getInverse(), this, inverseSoftware);
    em.persist(inverse);/* ww w  .  jav  a  2s. c  o m*/
}

From source file:org.nuxeo.ecm.platform.uidgen.service.UIDSequencerImpl.java

public int getNext(EntityManager em, String key) {
    UIDSequenceBean seq;/*from  w ww  .java  2 s .co  m*/
    try {
        seq = (UIDSequenceBean) em.createNamedQuery("UIDSequence.findByKey").setParameter("key", key)
                .getSingleResult();
        // createQuery("FROM UIDSequenceBean seq WHERE seq.key = :key")
    } catch (NoResultException e) {
        seq = new UIDSequenceBean(key);
        em.persist(seq);
    }
    return seq.nextIndex();
}

From source file:com.headissue.pigeon.survey.answer.AnswerHandler.java

UserMap createUserMap(Survey _survey, AnswerParameter p, EntityManager _manager) {
    UserMap map = new UserMap();
    map.setSurvey(_survey);/*from w ww  .j  a v  a 2  s. c  om*/
    map.setPageKey(p.getValues().getPageKey());
    map.setUserKey(p.getValues().getUserKey());
    map.setUserData(p.getValues().getUserData());
    map.setTimestamp(new Date(p.getTimestamp()));
    _manager.persist(map);
    return map;
}

From source file:test.unit.be.fedict.eid.dss.XmlSchemaManagerBeanTest.java

@Test
public void testAdd() throws Exception {
    // setup/*from w  w  w .  j a  v  a  2s  .c  o m*/
    XmlSchemaManagerBean testedInstance = new XmlSchemaManagerBean();
    InputStream xsdInputStream = XmlSchemaManagerBeanTest.class.getResourceAsStream("/example.xsd");
    EntityManager mockEntityManager = EasyMock.createMock(EntityManager.class);
    injectPersistenceContext(mockEntityManager, testedInstance);

    // expectations
    EasyMock.expect(mockEntityManager.find(XmlSchemaEntity.class, "urn:be:fedict:eid:dss:example"))
            .andReturn(null);

    Capture<XmlSchemaEntity> persistCapture = new Capture<XmlSchemaEntity>();
    mockEntityManager.persist(EasyMock.capture(persistCapture));

    // prepare
    EasyMock.replay(mockEntityManager);

    // operate
    testedInstance.add("1.0", xsdInputStream);

    // verify
    EasyMock.verify(mockEntityManager);
    XmlSchemaEntity resultEntity = persistCapture.getValue();
    assertEquals("urn:be:fedict:eid:dss:example", resultEntity.getNamespace());
    assertEquals("1.0", resultEntity.getRevision());
}

From source file:org.jasig.portal.events.aggr.JpaBaseAggregationDao.java

@AggrEventsTransactional
@Override//from  w  ww  .j av  a 2 s .c  o m
public final void updateAggregations(Iterable<T> aggregations, boolean removeFromCache) {
    final EntityManager entityManager = this.getEntityManager();

    for (final T aggregation : aggregations) {
        entityManager.persist(aggregation);

        if (removeFromCache) {
            this.hibernateCacheEvictor.evictEntity(aggregation.getClass(), aggregation.getId());
        }
    }
}

From source file:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java

@Test
public void testDeleteTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    troopC.setMission("mise C");
    entityManager.persist(troopC);
    entityManager.getTransaction().commit();
    entityManager.close();//from w ww.  j  a  v a  2  s .  co  m

    Troop t1 = troopDao.findTroopById(troopC.getId());
    troopDao.deleteTroop(t1);

    Troop t2 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t2, null);

}

From source file:org.nuxeo.theme.webwidgets.providers.PersistentProvider.java

public synchronized Widget createWidget(final String widgetTypeName) throws ProviderException {
    if (widgetTypeName == null) {
        throw new ProviderException("Widget type name is undefined");
    }/*from w  ww .ja v  a2s .c o  m*/

    try {
        return getPersistenceProvider().run(true, new RunCallback<Widget>() {
            public Widget runWith(EntityManager em) {
                final WidgetEntity widget = new WidgetEntity(widgetTypeName);
                em.persist(widget);
                return widget;
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }
}