Example usage for org.hibernate NonUniqueObjectException getEntityName

List of usage examples for org.hibernate NonUniqueObjectException getEntityName

Introduction

In this page you can find the example usage for org.hibernate NonUniqueObjectException getEntityName.

Prototype

public String getEntityName() 

Source Link

Usage

From source file:de.innovationgate.webgate.api.jdbc.WGDocumentImpl.java

License:Open Source License

/**
 * Performs an operation involving entity updates and catches possibly appearing {@link NonUniqueObjectException}s.
 * Those exceptions are solved by evicting every entity which causes it and retrying the operation.
 * @param task The update operation/* ww w  . ja  v a 2s. c o  m*/
 * @param session The hibernate session to use to load the entity
 * @param retries The number of operation retries before a {@link WGBackendException} exception is thrown to cancel the operation.
 * @throws Exception
 */
private void performUpdateOperation(Callable<Boolean> task, Session session, int retries, Object entity)
        throws Exception {

    int repeatCount = 0;
    while (true) {

        repeatCount++;
        if (repeatCount > 1000) {
            throw new WGBackendException(
                    "Update of document failed because of persistent duplicates more than 1000 times. Cancelling update.");
        }

        try {
            task.call();
            break;
        }

        // Another persistent version was loaded somehow. Evict it and try to update again.
        catch (NonUniqueObjectException e) {
            Object otherObject = session.load(e.getEntityName(), e.getIdentifier());
            session.evict(otherObject);
            if (entity != null) {
                session.evict(entity);
            }
        }
    }

}