Example usage for org.hibernate StatelessSession update

List of usage examples for org.hibernate StatelessSession update

Introduction

In this page you can find the example usage for org.hibernate StatelessSession update.

Prototype

void update(Object entity);

Source Link

Document

Update a row.

Usage

From source file:edu.psu.iam.cpr.core.database.postprocess.NamesPostProcessor.java

License:Creative Commons License

/**
 * This method is used to expire an old names bean, because a new one will be created.
 * @param db contains the database session.
 * @param bean contains the bean that will be expired.
 * @param updatedBy contains the updated by user.
 *//*from www .  j  a  v a  2s  . co m*/
public void expireName(final StatelessSession db, final Names bean, final String updatedBy) {
    final Date d = new Date();

    bean.setEndDate(d);
    bean.setLastUpdateBy(updatedBy);
    bean.setLastUpdateOn(d);
    db.update(bean);

    setOldNamesBean(bean);

}

From source file:edu.psu.iam.cpr.core.messaging.MessagingCore.java

License:Apache License

/**
 *
 * @param databaseSession contains the database stateless session
 * @param changeType contains the type of change notifications
 * @param msg contains the json message/*from w w  w. ja  v a2 s  .  c  om*/
 * @throws CprException
 * @throws JMSException
 * @throws JSONException
 */

public void sendNotification(StatelessSession databaseSession, ChangeNotificationType changeType,
        JsonMessage msg) throws CprException, JMSException, JSONException {

    final String replyToQueue = CprProperties.INSTANCE.getProperties()
            .getProperty(CprPropertyName.CPR_JMS_REPLYTO.toString());
    final List<VSpNotification> theDest = msgNotificationQueues.get(changeType.toString());

    MessageProducer msgSender = null;
    if (msg == null || msg.getJsonObject().toString().length() == 0) {
        closeMessaging();
        throw new CprException(ReturnType.MESSAGE_CREATION_EXCEPTION);
    }

    try {
        //Create the message to send to the queues
        TextMessage myTextMsg = null;

        myTextMsg = jmsSession.createTextMessage();

        // Loop through queue list
        final Iterator<VSpNotification> queueIter = theDest.iterator();
        while (queueIter.hasNext()) {

            VSpNotification currentDestination = queueIter.next();

            // Update the JSON message with the message consumer key, message type and change type
            msg.setValue(MessageKeyName.MESSAGE_CONSUMER_KEY, currentDestination.getMessageConsumerKey());
            msg.setValue(MessageKeyName.MESSAGE_TYPE, CprMessageType.CHANGE_NOTIFICATION.toString());
            msg.setValue(MessageKeyName.CHANGE_KEY, changeType.toString());

            // Store an entry in the message log.

            MessageLogTable messageLogTable = new MessageLogTable(currentDestination.getWebServiceKey(),
                    currentDestination.getMessageConsumerKey(), currentDestination.getServiceKey(),
                    msg.getJsonObject().toString(), msg.getRequestedBy());
            MessageLog mLogBean = messageLogTable.getMessageLogBean();
            databaseSession.insert(mLogBean);

            // Store an entry in the message log history.
            MessageLogHistoryTable messageLogHistoryTable = new MessageLogHistoryTable(
                    messageLogTable.getMessageLogBean());
            MessageLogHistory mLogHistory = messageLogHistoryTable.getMessageLogHistoryBean();
            databaseSession.insert(mLogHistory);

            // once the log is started, the messageLogId is set, add it to the msg to be sent
            msg.setValue(MessageKeyName.MESSAGE_LOG_ID, mLogBean.getMessageLogKey());

            // Add in the JSON information.
            myTextMsg.setText(msg.getJsonObject().toString());
            myTextMsg.setJMSReplyTo(jmsSession.createQueue(replyToQueue));
            myTextMsg.setJMSCorrelationID(mLogHistory.getMessageLogHistoryKey().toString());

            // Send the message
            Destination destination = jmsSession.createQueue(currentDestination.getConsumerDestination());
            msgSender = jmsSession.createProducer(destination);
            msgSender.setDeliveryMode(DeliveryMode.PERSISTENT);
            msgSender.send(myTextMsg);
            msgSender.close();

            // once the message is sent, add an entry in the message log history table for this specific send
            mLogBean.setSuccessFlag("Y");
            mLogBean.setNumberOfTries(1L);
            mLogBean.setLastUpdateOn(new Date());
            databaseSession.update(mLogBean);
            mLogHistory.setMessageId(myTextMsg.getJMSMessageID());
            databaseSession.update(mLogHistory);
            // Log the successful message delivery to the log4j component.
            logMessageDelivery(messageLogTable, "SUCCESS");
        }

    } finally {
        try {
            if (msgSender != null) {
                msgSender.close();
            }
        } catch (JMSException e) {

        }
    }
}

From source file:edu.utah.further.core.data.hibernate.listeners.PreUpdatePreventNullOverwriteListener.java

License:Apache License

@SuppressWarnings("resource")
@Override/*from   w w  w  .  j a  v  a 2s. c om*/
public boolean onPreUpdate(final PreUpdateEvent event) {
    if (log.isDebugEnabled()) {
        log.debug("Received pre-update event");
    }
    final EntityPersister entityPersister = event.getPersister();
    final EntityMode entityMode = entityPersister.guessEntityMode(event.getEntity());
    final Connection connection = getConnection(entityPersister);
    final StatelessSession session = entityPersister.getFactory().openStatelessSession(connection);
    session.beginTransaction();
    final Serializable entityId = entityPersister.getIdentifier(event.getEntity(),
            (SessionImplementor) session);
    final Object existingEntity = session.get(event.getEntity().getClass(), entityId);

    if (log.isDebugEnabled()) {
        log.debug("Loaded existing entity " + existingEntity);
    }

    boolean updatedExistingEntity = false;
    for (int i = 0; i < entityPersister.getPropertyNames().length; i++) {
        final Object oldPropValue = entityPersister.getPropertyValue(existingEntity, i, entityMode);
        if (oldPropValue == null) {

            if (log.isDebugEnabled()) {
                log.debug("Found a property in the existing " + "entity that was null, checking new entity");
            }

            final Object newPropValue = entityPersister.getPropertyValue(event.getEntity(), i, entityMode);

            if (!(newPropValue instanceof Collection<?>) && newPropValue != null) {

                if (log.isDebugEnabled()) {
                    log.debug("The new entity contains a non-null " + "value, updating existing entity");
                }

                entityPersister.setPropertyValue(existingEntity, i, newPropValue, entityMode);
                updatedExistingEntity = true;
            }
        }
    }

    if (updatedExistingEntity) {
        entityPersister.getFactory().getCurrentSession().cancelQuery();
        session.update(existingEntity);
    }

    session.getTransaction().commit();
    session.close();

    return updatedExistingEntity;
}

From source file:gov.nih.nci.cabig.caaers.dao.OrganizationDao.java

License:BSD License

public void saveAll(final List<Organization> organizations) {
    StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession();
    try {/*ww w  . ja va 2  s  .c  o  m*/
        for (Organization o : organizations) {
            log.info("processing :" + o.getNciInstituteCode());
            if (o.getId() == null) {
                if (log.isInfoEnabled())
                    log.info("Inserting " + o.getNciInstituteCode());
                session.insert(o);
            } else {
                if (log.isInfoEnabled())
                    log.info("Updating " + o.getNciInstituteCode());
                session.update(o);
            }
        }
    } catch (HibernateException e) {
        throw getHibernateTemplate().convertHibernateAccessException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        session.close();
    }
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public void saveCndFile(String filename, String content, Properties properties) throws RepositoryException {
    StatelessSession session = null;
    try {//from   w  w w . j  av  a2 s . c om
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        NodeTypesDBProvider nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", filename).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null && content != null) {
            nodeTypesDBProvider.setCndFile(content);
            session.update(nodeTypesDBProvider);
        } else if (nodeTypesDBProvider != null) {
            session.delete(nodeTypesDBProvider);
        } else if (content != null) {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(filename);
            nodeTypesDBProvider.setCndFile(content);
            session.insert(nodeTypesDBProvider);
        }

        final StringWriter writer = new StringWriter();
        properties.store(writer, "");

        nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", DEFINITIONS_PROPERTIES).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null) {
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.update(nodeTypesDBProvider);
        } else {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(DEFINITIONS_PROPERTIES);
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.insert(nodeTypesDBProvider);
        }

        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

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

License:Open Source License

private void executeWriteIfExists(StatelessSession session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();
    if (exists(bulkItem, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Update Entry [" + entry + "]");
        }//from w ww.j  a  v  a2 s.  c o m
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

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

License:Open Source License

private void executeUpdateIfExists(StatelessSession session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();
    if (exists(bulkItem, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Update Entry [" + entry + "]");
        }/*from w  ww. j  a va 2  s  .  c om*/
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

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

License:Open Source License

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

    if (logger.isTraceEnabled()) {
        logger.trace("[Optimistic UPDATE] Update Entry [" + entry + "]");
    }//from w w  w  .  j  a  v a2  s  .  co  m
    session.update(entry);
}

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

License:Open Source License

private void executeWriteIfExists(StatelessSession session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;/*w ww  .  j  a  va  2s  .c  o m*/

    Object entry = dataSyncOperation.getDataAsObject();
    if (exists(dataSyncOperation, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Update Entry [" + entry + "]");
        }
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

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

License:Open Source License

private void executeUpdateIfExists(StatelessSession session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;/*from w w w  .  ja  v  a2s .c o m*/

    Object entry = dataSyncOperation.getDataAsObject();
    if (exists(dataSyncOperation, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Update Entry [" + entry + "]");
        }
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}