Example usage for org.springframework.orm.hibernate5 HibernateOperations saveOrUpdate

List of usage examples for org.springframework.orm.hibernate5 HibernateOperations saveOrUpdate

Introduction

In this page you can find the example usage for org.springframework.orm.hibernate5 HibernateOperations saveOrUpdate.

Prototype

void saveOrUpdate(Object entity) throws DataAccessException;

Source Link

Document

Save or update the given persistent instance, according to its id (matching the configured "unsaved-value"?).

Usage

From source file:org.springframework.batch.item.database.HibernateItemWriter.java

/**
 * Do perform the actual write operation using {@link HibernateOperations}.
 * This can be overridden in a subclass if necessary.
 *
 * @param hibernateTemplate//from   w  ww  .ja va  2 s  .  c  om
 *            the HibernateTemplate to use for the operation
 * @param items
 *            the list of items to use for the write
 * @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly
 */
@Deprecated
protected void doWrite(HibernateOperations hibernateTemplate, List<? extends T> items) {

    if (logger.isDebugEnabled()) {
        logger.debug("Writing to Hibernate with " + items.size() + " items.");
    }

    if (!items.isEmpty()) {
        long saveOrUpdateCount = 0;
        for (T item : items) {
            if (!hibernateTemplate.contains(item)) {
                hibernateTemplate.saveOrUpdate(item);
                saveOrUpdateCount++;
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug(saveOrUpdateCount + " entities saved/updated.");
            logger.debug((items.size() - saveOrUpdateCount) + " entities found in session.");
        }
    }

}