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.apache.juddi.query.EntityQuery.java

public static void storeIntermediateKeySetResults(EntityManager em, String txId, List<?> keysIn) {

    for (Object key : keysIn) {
        TempKey tempKey = new TempKey();
        tempKey.setPk(txId, key.toString());
        em.persist(tempKey);
    }/*from   w w  w  .j a  v  a  2 s  . com*/
}

From source file:org.opentides.dao.impl.AuditLogDaoImpl.java

/**
 * Saves the log event into the database.
 * @param shortMessage//w w  w  . j  a  v  a 2 s.  c om
 * @param message
 * @param entity
 */
public static void logEvent(String message, BaseEntity entity) {
    Long userId = entity.getAuditUserId();
    String username = entity.getAuditUsername();

    if (ApplicationStartupListener.isApplicationStarted()) {
        if (userId == null) {
            _log.error("No userId specified for audit logging on object [" + entity.getClass().getName()
                    + "] for message [" + message + "]. Retrieving user from interceptor.");
            SessionUser user = SecurityUtil.getSessionUser();
            userId = user.getId();
            username = user.getUsername();
        }
    } else {
        userId = new Long(0);
        username = "System Evolve";
    }

    EntityManager em = DatabaseUtil.getEntityManager();
    try {
        em.getTransaction().begin();
        AuditLog record = new AuditLog(message, entity.getId(), entity.getClass(), entity.getReference(),
                userId, username);
        em.persist(record);
        em.flush();
        em.getTransaction().commit();
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opentides.persistence.interceptor.SynchronizableInterceptor.java

/**
 * Saves the change log into the database.
 * @param shortMessage/*from   www.ja  va 2 s. c  o  m*/
 * @param message
 * @param entity
 */
public static void saveLog(BaseEntity entity, int action, String updateFields, String sqlCommand) {
    EntityManager em = DatabaseUtil.getEntityManager();
    try {
        em.getTransaction().begin();
        ChangeLog cl = new ChangeLog(entity.getId(), entity.getClass(), action, updateFields, sqlCommand);
        em.persist(cl);
        em.flush();
        em.getTransaction().commit();
    } catch (Exception ex) {
        _log.error("Failed to save change log on [" + entity.getClass().getSimpleName() + "]", ex);
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:com.siberhus.ngai.core.CrudHelper.java

@SuppressWarnings("unchecked")
public final static Object save(EntityManager em, Object model) {
    if (em == null) {
        throw new IllegalArgumentException("EntityManager is null");
    }/*from   w  ww. j ava 2s .  c  o m*/
    if (model instanceof IModel) {
        if (((IModel<Serializable>) (model)).isNew()) {
            em.persist(model);
            return model;
        }
    } else {
        Object id = null;
        try {
            id = PropertyUtils.getProperty(model, "id");
        } catch (Exception e) {
            throw new NgaiRuntimeException("Unable to find property 'id' fom model: " + model, e);
        }
        if (id == null) {
            em.persist(model);
            return model;
        }
    }
    return em.merge(model);
}

From source file:org.sigmah.server.endpoint.export.sigmah.handler.ProjectReportModelHandler.java

/**
 * Save the key questions of a section//from   w w  w. j  a  v a  2s. c  o  m
 * 
 * @param section
 *            the section
 * @param keyQuestions
 *            the key questions of the sections
 * @param em
 *            the entity manager
 */
private static void saveSectionKeyQuestion(ProjectReportModelSection section, List<KeyQuestion> keyQuestions,
        EntityManager em) {
    for (KeyQuestion keyQuestion : keyQuestions) {
        keyQuestion.setSectionId(section.getId());
        if (keyQuestion.getQualityCriterion() != null) {
            saveKeyQuestionQualityCriterion(keyQuestion.getQualityCriterion(), em);
        }
        em.persist(keyQuestion);
    }
    section.setKeyQuestions(keyQuestions);
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Save a new access point to the database.
 * @param ap The access point to be saved.
 *//*from   ww  w.  j  a  va  2  s .com*/
public static void saveAccessPoint(final AccessPoint ap) {
    EntityManager em = DBContext.getEntityManager();
    em.getTransaction().begin();
    em.persist(ap);
    em.getTransaction().commit();
    em.close();
}

From source file:com.github.jinahya.persistence.ShadowTest.java

protected static Shadow persistInstance(final EntityManager manager, String username, byte[] password) {

    if (username == null) {
        username = newUsername(manager);
    }/*from  w  w  w .ja  va 2  s.  com*/

    if (password == null) {
        password = newPassword();
    }

    final Shadow instance = Shadow.newInstance(username, password);

    manager.persist(instance);

    return instance;
}

From source file:org.sigmah.server.endpoint.export.sigmah.handler.ProjectReportModelHandler.java

/**
 * Save elements of imported project report model
 * /* www. j a  va2  s.  c  o  m*/
 * @param projectReportModel
 *            the imported project report model
 * @param em
 *            the entity manager
 */
public static void saveProjectReportModelElement(ProjectReportModel projectReportModel, EntityManager em) {

    //Save the sections on the project report model
    List<ProjectReportModelSection> sections = projectReportModel.getSections();
    if (sections != null) {
        // Save the project report model without the sections;
        projectReportModel.setSections(null);
        em.persist(projectReportModel);
        // Save the project report sections with the parent project report (saved above)
        for (ProjectReportModelSection section : sections) {
            section.setProjectModelId(projectReportModel.getId());
            List<ProjectReportModelSection> subSections = section.getSubSections();
            List<KeyQuestion> keyQuestions = section.getKeyQuestions();
            if (subSections != null || keyQuestions != null) {
                //Save the section without the sub sections or the key questions
                section.setSubSections(null);
                section.setKeyQuestions(null);
                em.persist(section);
                //Save the sub sections and the key questions
                saveSectionSubSectionKeyQuestions(section, subSections, keyQuestions, em);
                section.setSubSections(subSections);

                em.merge(section);
            } else {
                em.persist(section);
            }
        }
        // Set the sections saved above to the project report model
        projectReportModel.setSections(sections);
    }
}

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

/**
 * Save elements of imported project report model
 * /*ww w.  ja v a  2s  .c  om*/
 * @param projectReportModel
 *          the imported project report model
 * @param em
 *          the entity manager
 */
public static void saveProjectReportModelElement(ProjectReportModel projectReportModel, EntityManager em) {

    // Save the sections on the project report model
    List<ProjectReportModelSection> sections = projectReportModel.getSections();
    if (sections != null) {
        // Save the project report model without the sections;
        projectReportModel.setSections(null);
        em.persist(projectReportModel);
        // Save the project report sections with the parent project report (saved above)
        for (ProjectReportModelSection section : sections) {
            section.setProjectModelId(projectReportModel.getId());
            List<ProjectReportModelSection> subSections = section.getSubSections();
            List<KeyQuestion> keyQuestions = section.getKeyQuestions();
            if (subSections != null || keyQuestions != null) {
                // Save the section without the sub sections or the key questions
                section.setSubSections(null);
                section.setKeyQuestions(null);
                em.persist(section);
                // Save the sub sections and the key questions
                saveSectionSubSectionKeyQuestions(section, subSections, keyQuestions, em);
                section.setSubSections(subSections);

                em.merge(section);
            } else {
                em.persist(section);
            }
        }
        // Set the sections saved above to the project report model
        projectReportModel.setSections(sections);
    }
}

From source file:org.sigmah.server.endpoint.export.sigmah.handler.ProjectReportModelHandler.java

/**
 * Save the quality criterion passed in argument.
 * //  w  w  w . j  av  a  2s.c  o m
 * @param qualityCriterion
 *            the quality criterion to save
 * @param em
 *            the entity manager
 */
private static void saveKeyQuestionQualityCriterion(QualityCriterion qualityCriterion, EntityManager em) {
    List<QualityCriterion> qualityCriterions = qualityCriterion.getSubCriteria();
    QualityFramework qualityFramework = qualityCriterion.getQualityFramework();
    if (qualityCriterions != null || qualityFramework != null) {
        qualityCriterion.setSubCriteria(null);
        qualityCriterion.setQualityFramework(null);
        em.persist(qualityCriterion);
        for (QualityCriterion criterion : qualityCriterions) {
            saveKeyQuestionQualityCriterion(criterion, em);
        }
        qualityCriterion.setSubCriteria(qualityCriterions);
        em.persist(qualityFramework);
        qualityCriterion.setQualityFramework(qualityFramework);
        em.merge(qualityCriterion);
    } else {
        em.persist(qualityCriterion);
    }
}