Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

In this page you can find the example usage for org.hibernate Session saveOrUpdate.

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:ar.com.hibernate.dao.Dao.java

public void grabar(E entity) {

    Session session = null;
    Transaction transaction = null;/*from w  w w .  j  a v  a  2  s .c  om*/
    try {
        session = SessionManager.getSessionFactory().openSession();
        transaction = session.beginTransaction();
        // el metodo saveOrUpdate realiza automaticamente la operacion correcta
        session.saveOrUpdate(entity);
        transaction.commit();

    } catch (HibernateException ex) {
        if (transaction != null) {
            transaction.rollback(); // Realiza un rollback si re realiza una sucesion de operaciones en la base y alguna no se cumple correctamente.
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:at.gv.egovernment.moa.id.commons.db.MOASessionDBUtils.java

License:EUPL

public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException {
    Transaction tx = null;/*  w w  w.j  av a 2  s . c  o m*/
    try {
        Session session = MOASessionDBUtils.getCurrentSession();

        synchronized (session) {
            tx = session.beginTransaction();
            session.saveOrUpdate(dbo);
            tx.commit();
        }
        return true;

    } catch (HibernateException e) {
        Logger.warn("Error during MOASession database saveOrUpdate. Rollback.", e);
        tx.rollback();
        throw new MOADatabaseException(e);
    }
}

From source file:at.gv.egovernment.moa.id.commons.db.StatisticLogDBUtils.java

License:EUPL

public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException {
    Transaction tx = null;//from   w ww . ja v a 2  s.co  m
    try {
        Session session = StatisticLogDBUtils.getCurrentSession();

        synchronized (session) {
            tx = session.beginTransaction();
            session.saveOrUpdate(dbo);
            tx.commit();
        }

        Logger.info("Insert advanced statistic log entry into database");
        return true;

    } catch (HibernateException e) {
        Logger.warn("Error during StatisicLogger database saveOrUpdate. Rollback.", e);
        tx.rollback();
        throw new MOADatabaseException(e);
    }
}

From source file:at.gv.egovernment.moa.id.storage.AuthenticationSessionStoreage.java

License:EUPL

public static void addSSOInformation(String moaSessionID, String SSOSessionID, SLOInformationInterface SLOInfo,
        String OAUrl) throws AuthenticationException {

    AuthenticatedSessionStore dbsession;
    Transaction tx = null;//from  w w w  .  ja v  a  2 s .c o  m

    try {

        Session session = MOASessionDBUtils.getCurrentSession();
        List<AuthenticatedSessionStore> result;

        Logger.trace("Add SSO information to session " + moaSessionID);

        synchronized (session) {

            tx = session.beginTransaction();
            Query query = session.getNamedQuery("getSessionWithID");
            query.setParameter("sessionid", moaSessionID);
            result = query.list();

            Logger.trace("Found entries: " + result.size());

            //Assertion requires an unique artifact
            if (result.size() != 1) {
                Logger.trace("No entries found.");
                tx.rollback();
                throw new MOADatabaseException("No session found with this sessionID");
            }

            dbsession = (AuthenticatedSessionStore) result.get(0);

            OASessionStore activeOA = null;
            //check if OA already has an active OA session
            if (dbsession.getActiveOAsessions() != null) {
                for (OASessionStore el : dbsession.getActiveOAsessions()) {
                    if (el.getOaurlprefix().equals(OAUrl))
                        activeOA = el;
                }
            }

            if (activeOA == null)
                activeOA = new OASessionStore();

            //set active OA applications
            activeOA.setOaurlprefix(OAUrl);
            activeOA.setMoasession(dbsession);
            activeOA.setCreated(new Date());

            //set additional information for SLO
            if (SLOInfo != null) {
                activeOA.setAssertionSessionID(SLOInfo.getSessionIndex());
                activeOA.setUserNameID(SLOInfo.getUserNameIdentifier());
                activeOA.setUserNameIDFormat(SLOInfo.getUserNameIDFormat());
                activeOA.setProtocolType(SLOInfo.getProtocolType());
                activeOA.setAttributeQueryUsed(false);

            }

            List<OASessionStore> activeOAs = dbsession.getActiveOAsessions();
            activeOAs.add(activeOA);
            dbsession.setActiveOAsessions(activeOAs);

            //Store used SSOId 
            if (dbsession.getSSOsessionid() != null) {
                OldSSOSessionIDStore oldSSOId = new OldSSOSessionIDStore();
                oldSSOId.setOldsessionid(dbsession.getSSOsessionid());
                oldSSOId.setMoasession(dbsession);

                List<OldSSOSessionIDStore> oldSSOIds = dbsession.getOldssosessionids();
                oldSSOIds.add(oldSSOId);
            }

            dbsession.setSSOSession(true);
            dbsession.setSSOsessionid(SSOSessionID);
            dbsession.setAuthenticated(false);
            dbsession.setPendingRequestID("empty");

            //Store MOASession
            session.saveOrUpdate(dbsession);

            //send transaction
            tx.commit();

            Logger.debug("Add SSO-Session login information for OA: " + OAUrl + " and AssertionID: "
                    + SLOInfo.getSessionIndex());
        }

    } catch (MOADatabaseException e) {
        throw new AuthenticationException("No MOASession found with Id=" + moaSessionID, null);

    } catch (HibernateException e) {
        Logger.warn("Error during database saveOrUpdate. Rollback.", e);
        tx.rollback();
        throw new AuthenticationException("SSO Session information can not be stored!  --> SSO is deactivated",
                null);
    }
}

From source file:au.com.nicta.ct.db.CtKeyValueProperties.java

License:Open Source License

public static void setValue(String key, String value) {
    CtProperties p = find(key);/*  w  w w .jav a  2s.  c o m*/

    if (p == null) {
        persist(key, value); // will cache

        return;
    }

    //x        Session s = CtSession.Current();
    Session s = CtSession.Create();

    try {
        p.setValue(value);
        s.saveOrUpdate(p);
        s.flush();
    } catch (Exception e) {
    } finally {
        s.close();
    }

    if (useReadCaching) {
        od.put(key, value);
    }
}

From source file:au.com.nicta.ct.db.entities.CtEntityPropertiesUtil.java

License:Open Source License

public static void setValue(Session s, CtSolutions ctSolutions, Class entityName, Integer entityPk,
        String propertyName, String propertyValue) {

    List<CtEntityProperties> l = find(s, ctSolutions, entityName, entityPk, propertyName);

    if (l.isEmpty()) {
        persist(s, ctSolutions, entityName, entityPk, propertyName, propertyValue);
        return;//from w  w w  .j  av  a2 s. c o  m
    }

    if (l.size() != 1) {
        for (CtEntityProperties ep : l) {
            System.out.println("ep.getName(): " + (ep.getName()));
        }
        throw new Error("Duplicate entity properties entry, " + " Table: " + getClassName(entityName) + " Row: "
                + entityPk + " Property name: " + propertyName);
    }

    CtEntityProperties ep = l.get(0);

    ep.setValue(propertyValue);
    s.saveOrUpdate(ep);
}

From source file:au.gov.naa.digipres.spyd.dao.hibernate.HibernateItemRecordDAO.java

License:Open Source License

@Override
public void saveItemRecord(ItemRecord itemRecord) {
    logger.fine("Persisting Item Record");
    Session session = HibernateUtil.getSession();
    session.saveOrUpdate(itemRecord);
    session.flush();// ww w.j  a v  a 2  s .  co m
    session.clear();
    logger.fine("Item Record Persisted.");
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

/**
 * Create a new Ark User in the system and associates the arkuser with the study and links the user to one or more Modules and Roles that was
 * configured for the Study./*  w  ww.  j av a  2 s  . co  m*/
 */
public void createArkUser(ArkUserVO arkUserVO) {
    Session session = getSession();
    session.save(arkUserVO.getArkUserEntity());
    List<ArkUserRole> arkUserRoleList = arkUserVO.getArkUserRoleList();
    for (ArkUserRole arkUserRole : arkUserRoleList) {
        if (arkUserRole.getArkRole() != null) {
            arkUserRole.setArkUser(arkUserVO.getArkUserEntity());
            session.save(arkUserRole);
        }
    }

    for (UserConfig config : arkUserVO.getArkUserConfigs()) {
        session.saveOrUpdate(config);
    }
}

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

/**
 * Update CustomFieldGroup and its related CustomFields(Add or remove)
 *//*from   ww  w .ja  va2 s .  co  m*/
public void updateCustomFieldGroup(CustomFieldGroupVO customFieldGroupVO)
        throws EntityExistsException, ArkSystemException {

    CustomFieldGroup customFieldGroup = customFieldGroupVO.getCustomFieldGroup();
    Session session = getSession();
    session.update(customFieldGroup);//Update

    if (!customFieldGroup.getPublished()) {//Allow Removal only if the form is not published
        Collection<CustomFieldDisplay> customFieldDisplayToRemove = getCustomFieldDisplayToRemove(
                customFieldGroupVO.getSelectedCustomFields(), customFieldGroup);
        for (CustomFieldDisplay cfd : customFieldDisplayToRemove) {
            session.delete(cfd);
        }
    }

    ArrayList<CustomFieldDisplay> customFieldsToAdd = getCustomFieldsToAdd(
            customFieldGroupVO.getSelectedCustomFields(), customFieldGroup);
    for (CustomFieldDisplay fieldToAdd : customFieldsToAdd) {
        session.saveOrUpdate(fieldToAdd);//Add a new CustomFieldDisplay field that is linked to the CustomField   
    }

    ArrayList<CustomField> list = customFieldGroupVO.getSelectedCustomFields();
    int position = 0;

    for (CustomField customField : list) {
        ++position;
        CustomFieldDisplay cfd = iArkCommonService.getCustomFieldDisplayByCustomField(customField,
                customFieldGroupVO.getCustomFieldGroup());
        cfd.setSequence(new Long(position));
        session.update(cfd);
    }

}

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

@Override
public void updatePhenoDataSetFieldGroup(PhenoDataSetFieldGroupVO phenoDataSetFieldGroupVO)
        throws EntityExistsException, ArkSystemException {
    PhenoDataSetGroup phenoDataSetGroup = phenoDataSetFieldGroupVO.getPhenoDataSetGroup();
    Session session = getSession();
    session.saveOrUpdate(phenoDataSetGroup);//Update phenoDataSetGroup
    Collection<PhenoDataSetFieldDisplay> phenoDataSetFieldDisplayToRemove = getPhenoFieldDisplayToRemove(
            phenoDataSetGroup);//ww w  .ja  v a  2  s .c  o m
    for (PhenoDataSetFieldDisplay phenoDataSetFieldDisplay : phenoDataSetFieldDisplayToRemove) {
        session.delete(phenoDataSetFieldDisplay);
        session.flush();
    }
    insertToDispalyAndDeleteFromLinkAndPicked(phenoDataSetFieldGroupVO, phenoDataSetGroup, session);
    log.debug("Update PhenoDataSetFieldDisplay for PhenoDataSet Group");

}