Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncDAO.java

License:Open Source License

/**
 * @see org.openmrs.module.sync.api.db.SyncDAO#saveRemoteServer(org.openmrs.module.sync.engine.RemoteServer)
 */// ww w.  ja va  2 s.  c o  m
public RemoteServer saveRemoteServer(RemoteServer server) throws DAOException {
    Session session = sessionFactory.getCurrentSession();
    return (RemoteServer) session.merge(server);
}

From source file:org.openspaces.persistency.hibernate.DefaultHibernateExternalDataSource.java

License:Open Source License

private void executeWrite(Session session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();

    if (logger.isTraceEnabled()) {
        logger.trace("Write Entry [" + entry + ']');
    }/*from ww  w  .j a  va 2  s  .c  o  m*/
    if (useMerge) {
        session.merge(entry);
    } else {
        try {
            session.saveOrUpdate(entry);
        } catch (HibernateException e) {
            session.merge(entry);
        }
    }

}

From source file:org.openspaces.persistency.hibernate.DefaultHibernateExternalDataSource.java

License:Open Source License

private void executeUpdate(Session session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();

    if (logger.isTraceEnabled()) {
        logger.trace("Update Entry [" + entry + ']');
    }/*  ww w . j ava 2s.c  o  m*/
    if (useMerge) {
        session.merge(entry);
    } else {
        try {
            session.saveOrUpdate(entry);
        } catch (HibernateException e) {
            session.merge(entry);
        }
    }

}

From source file:org.openspaces.persistency.hibernate.DefaultHibernateExternalDataSource.java

License:Open Source License

private void executeRemove(Session session, BulkItem bulkItem) throws DataSourceException {
    Object entry = bulkItem.getItem();

    if (logger.isTraceEnabled()) {
        logger.trace("Deleting Entry [" + entry + ']');
    }//w  ww.  j  a v  a  2 s  . c  o m

    if (deleteById) {

        Serializable id = (Serializable) (bulkItem.supportsGetSpaceId() ? bulkItem.getSpaceId()
                : getIdentifier(entry));
        if (id == null)
            throw new DataSourceException(
                    "Object id is null. Make sure object space id and hibernate id are the same property.");

        // ignore non existing objects - avoid unnecessary failures                            
        try {
            Object toDelete = session.load(entry.getClass(), id);

            if (toDelete != null)
                session.delete(toDelete);
        } catch (ObjectNotFoundException e) {
            // ignore non existing objects - avoid unnecessary failures
            if (logger.isTraceEnabled()) {
                logger.trace("Delete Entry failed [" + entry + ']', e);
            }
        }

    } else {
        if (useMerge) {
            session.delete(session.merge(entry));
        } else {
            try {
                session.delete(entry);
            } catch (HibernateException e) {
                session.delete(session.merge(entry));
            }
        }
    }

}

From source file:org.openspaces.persistency.hibernate.DefaultHibernateSpaceSynchronizationEndpoint.java

License:Open Source License

private void executeWrite(Session session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;/*from   w  w  w. j  a v  a  2  s.c o  m*/

    Object entry = dataSyncOperation.getDataAsObject();

    if (logger.isTraceEnabled()) {
        logger.trace("Write Entry [" + entry + ']');
    }
    if (useMerge) {
        session.merge(entry);
    } else {
        try {
            session.saveOrUpdate(entry);
        } catch (HibernateException e) {
            session.merge(entry);
        }
    }

}

From source file:org.openspaces.persistency.hibernate.DefaultHibernateSpaceSynchronizationEndpoint.java

License:Open Source License

private void executeUpdate(Session session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;/*from  w w  w  . ja  v a  2 s.c o m*/

    Object entry = dataSyncOperation.getDataAsObject();

    if (logger.isTraceEnabled()) {
        logger.trace("Update Entry [" + entry + ']');
    }
    if (useMerge) {
        session.merge(entry);
    } else {
        try {
            session.saveOrUpdate(entry);
        } catch (HibernateException e) {
            session.merge(entry);
        }
    }

}

From source file:org.openspaces.persistency.hibernate.DefaultHibernateSpaceSynchronizationEndpoint.java

License:Open Source License

private void executeRemove(Session session, DataSyncOperation dataSyncOperation) throws DataSourceException {
    if (!dataSyncOperation.supportsDataAsObject())
        return;/*  w  w w  .ja  v  a 2 s. c  o m*/

    Object entry = dataSyncOperation.getDataAsObject();

    if (logger.isTraceEnabled()) {
        logger.trace("Deleting Entry [" + entry + ']');
    }

    if (deleteById) {
        Serializable id = (Serializable) (dataSyncOperation.supportsGetSpaceId()
                ? dataSyncOperation.getSpaceId()
                : getIdentifier(entry));
        if (id == null)
            throw new DataSourceException(
                    "Object id is null. Make sure object space id and hibernate id are the same property.");

        // ignore non existing objects - avoid unnecessary failures                            
        try {
            Object toDelete = session.load(entry.getClass(), id);

            if (toDelete != null)
                session.delete(toDelete);
        } catch (ObjectNotFoundException e) {
            // ignore non existing objects - avoid unnecessary failures
            if (logger.isTraceEnabled()) {
                logger.trace("Delete Entry failed [" + entry + ']', e);
            }
        }

    } else {
        if (useMerge) {
            session.delete(session.merge(entry));
        } else {
            try {
                session.delete(entry);
            } catch (HibernateException e) {
                session.delete(session.merge(entry));
            }
        }
    }
}

From source file:org.pentaho.platform.repository.datasource.DatasourceMgmtService.java

License:Open Source License

public void deleteDatasource(IDatasource datasource)
        throws NonExistingDatasourceException, DatasourceMgmtServiceException {
    Session session = HibernateUtil.getSession();
    if (datasource != null) {
        try {//from   w w  w .j ava  2  s.  co m
            session.setCacheMode(CacheMode.REFRESH);
            session.delete(session.merge(datasource));
        } catch (HibernateException ex) {
            throw new DatasourceMgmtServiceException(ex.getMessage(), ex);
        } finally {
            session.setCacheMode(CacheMode.NORMAL);
        }
    } else {
        throw new DatasourceMgmtServiceException(
                Messages.getErrorString("DatasourceMgmtService.ERROR_0010_NULL_DATASOURCE_OBJECT"));//$NON-NLS-1$
    }

    HibernateUtil.flushSession();
}

From source file:org.pentaho.platform.repository.datasource.DatasourceMgmtService.java

License:Open Source License

public void updateDatasource(IDatasource datasource)
        throws NonExistingDatasourceException, DatasourceMgmtServiceException {
    Session session = HibernateUtil.getSession();
    if (datasource != null) {
        IDatasource tmpDatasource = getDatasource(datasource.getName());
        if (tmpDatasource != null) {
            try {
                session.setCacheMode(CacheMode.REFRESH);
                IPasswordService passwordService = PentahoSystem.getObjectFactory().get(IPasswordService.class,
                        null);/*  w  ww .  ja  va2s . com*/
                // Store the new encrypted password in the datasource object
                datasource.setPassword(passwordService.encrypt(datasource.getPassword()));

                // BISERVER-5677 - clear the old datasource from the datasource service cache so updates will be available
                // without having to restart the server
                IDatasourceService datasourceService = PentahoSystem.getObjectFactory()
                        .get(IDatasourceService.class, null);
                datasourceService.clearDataSource(datasource.getName());

                session.update(session.merge(datasource));
            } catch (ObjectFactoryException objface) {
                throw new DatasourceMgmtServiceException(Messages.getErrorString(
                        "DatasourceMgmtService.ERROR_0009_UNABLE_TO_INIT_PASSWORD_SERVICE"), objface);//$NON-NLS-1$
            } catch (PasswordServiceException pse) {
                throw new DatasourceMgmtServiceException(
                        Messages.getErrorString("DatasourceMgmtService.ERROR_0007_UNABLE_TO_ENCRYPT_PASSWORD"), //$NON-NLS-1$
                        pse);
            } catch (HibernateException ex) {
                throw new DatasourceMgmtServiceException(Messages.getErrorString(
                        "DatasourceMgmtService.ERROR_0004_UNABLE_TO_RETRIEVE_DATASOURCE", datasource.getName()), //$NON-NLS-1$
                        ex);
            } finally {
                session.setCacheMode(CacheMode.NORMAL);
            }
        } else {
            throw new NonExistingDatasourceException(Messages.getErrorString(
                    "DatasourceMgmtService.ERROR_0006_DATASOURCE_DOES_NOT_EXIST", datasource.getName()));//$NON-NLS-1$
        }
    } else {
        throw new DatasourceMgmtServiceException(
                Messages.getErrorString("DatasourceMgmtService.ERROR_0010_NULL_DATASOURCE_OBJECT"));//$NON-NLS-1$
    }
}

From source file:org.processbase.engine.bam.db.HibernateUtil.java

License:Open Source License

public MetaDim addMetaDim(MetaDim metaDim) {
    Session session = getSessionFactory().openSession();
    Transaction tx = null;/* w w w .  j  av  a  2 s.c  o  m*/
    try {
        tx = session.beginTransaction();
        metaDim = (MetaDim) session.merge(metaDim);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        session.close();
    }
    return metaDim;
}