org.hibernate.StaleObjectStateException Java Examples

The following examples show how to use org.hibernate.StaleObjectStateException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BusinessGroupDaoImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
*/
  @Override
  public void deleteBusinessGroup(BusinessGroup businessGroupTodelete) {
      try {
          // Delete the group object itself on the database
          db.deleteObject(businessGroupTodelete);
      } catch (final DBRuntimeException dbre) {
          final Throwable th = dbre.getCause();
          if ((th instanceof StaleObjectStateException) && (th.getMessage().startsWith("Row was updated or deleted by another transaction"))) {
              // known issue OLAT-3654
              log.info("Group was deleted by another user in the meantime. Known issue OLAT-3654");
              throw new KnownIssueException("Group was deleted by another user in the meantime", 3654);
          } else {
              throw dbre;
          }
      }
  }
 
Example #2
Source File: DefaultFlushEntityEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) {
	if ( persister.isSelectBeforeUpdateRequired() ) {
		Object[] snapshot = session.getPersistenceContext()
				.getDatabaseSnapshot(id, persister);
		if (snapshot==null) {
			//do we even really need this? the update will fail anyway....
			if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatisticsImplementor()
						.optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
		else {
			return snapshot;
		}
	}
	else {
		//TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
		EntityKey entityKey = new EntityKey( id, persister, session.getEntityMode() );
		return session.getPersistenceContext()
				.getCachedDatabaseSnapshot( entityKey ); 
	}
}
 
Example #3
Source File: OptimisticLockingOneRootOneVersionTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                post.setTitle("JPA");
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Alice: Optimistic locking failure", expected);
    }
    aliceLatch.countDown();
}
 
Example #4
Source File: OptimisticLockingOneRootOneVersionTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                aliceLatch.await();
                post.setViews(15);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Carol: Optimistic locking failure", expected);
    }
}
 
Example #5
Source File: LockModePessimisticReadWriteIntegrationTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
private void testPessimisticLocking(LockRequestCallable primaryLockRequestCallable, LockRequestCallable secondaryLockRequestCallable) {
    doInJPA(entityManager -> {
        try {
            Session session = entityManager.unwrap(Session.class);
            Post post = entityManager.find(Post.class, 1L);
            primaryLockRequestCallable.lock(session, post);
            executeAsync(
                    () -> {
                        doInJPA(_entityManager -> {
                            Session _session = _entityManager.unwrap(Session.class);
                            Post _post = _entityManager.find(Post.class, 1L);
                            secondaryLockRequestCallable.lock(_session, _post);
                        });
                    },
                    endLatch::countDown
            );
            sleep(WAIT_MILLIS);
        } catch (StaleObjectStateException e) {
            LOGGER.info("Optimistic locking failure: ", e);
        }
    });
    awaitOnLatch(endLatch);
}
 
Example #6
Source File: OptimisticLockingRepeatableReadTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                entityManager.unwrap(Session.class).doWork(connection -> {
                    assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation());
                });

                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                post.setTitle("JPA");
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Alice: Optimistic locking failure", expected);
    } catch (Exception unexpected) {
        LOGGER.info("Alice: Optimistic locking failure due to MVCC", unexpected);
    }
    aliceLatch.countDown();
}
 
Example #7
Source File: OptimisticLockingRepeatableReadTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                entityManager.unwrap(Session.class).doWork(connection -> {
                    assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation());
                });
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                aliceLatch.await();
                post.incrementLikes();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Bob: Optimistic locking failure", expected);
    } catch (Exception unexpected) {
        LOGGER.info("Bob: Optimistic locking failure due to MVCC", unexpected);
    }
}
 
Example #8
Source File: OptimisticLockingOneRootDirtyVersioningTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                post.setTitle("JPA");
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Alice: Optimistic locking failure", expected);
    }
    aliceLatch.countDown();
}
 
Example #9
Source File: OptimisticLockingOneRootDirtyVersioningTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                aliceLatch.await();
                post.incrementLikes();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Bob: Optimistic locking failure", expected);
    }
}
 
Example #10
Source File: OptimisticLockingOneRootDirtyVersioningTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                aliceLatch.await();
                post.setViews(15);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Carol: Optimistic locking failure", expected);
    }
}
 
Example #11
Source File: LockModePessimisticForceIncrementTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcurrentPessimisticForceIncrementLockingFailFast() {
    try {
        doInJPA(entityManager -> {
            Repository repository = entityManager.find(Repository.class, 1L);

            executeSync(() -> {
                doInJPA(_entityManager -> {
                    Repository _repository = _entityManager.find(Repository.class, 1L,
                        LockModeType.PESSIMISTIC_FORCE_INCREMENT);

                    Commit _commit = new Commit(_repository);
                    _commit.getChanges().add(new Change("Intro.md", "0a1,2..."));

                    _entityManager.persist(_commit);
                });
            });

            entityManager.lock(repository, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
            fail("Should have thrown StaleObjectStateException!");
        });
    } catch (OptimisticLockException expected) {
        assertEquals(StaleObjectStateException.class, expected.getCause().getClass());
        LOGGER.info("Failure: ", expected);
    }
}
 
Example #12
Source File: OptimisticLockingOneRootOneVersionTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInJPA(entityManager -> {
            try {
                Post post = entityManager.find(Post.class, 1L);
                loadPostLatch.countDown();
                loadPostLatch.await();
                aliceLatch.await();
                post.incrementLikes();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Bob: Optimistic locking failure", expected);
    }
}
 
Example #13
Source File: BusinessGroupDaoImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
*/
  @Override
  public void deleteBusinessGroup(BusinessGroup businessGroupTodelete) {
      try {
          // Delete the group object itself on the database
          db.deleteObject(businessGroupTodelete);
      } catch (final DBRuntimeException dbre) {
          final Throwable th = dbre.getCause();
          if ((th instanceof StaleObjectStateException) && (th.getMessage().startsWith("Row was updated or deleted by another transaction"))) {
              // known issue OLAT-3654
              log.info("Group was deleted by another user in the meantime. Known issue OLAT-3654");
              throw new KnownIssueException("Group was deleted by another user in the meantime", 3654, dbre);
          } else {
              throw dbre;
          }
      }
  }
 
Example #14
Source File: OptimisticLockingOneRootOneVersionTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInTransaction(session -> {
            try {
                Product product = (Product) session.get(Product.class, 1L);
                loadProductsLatch.countDown();
                loadProductsLatch.await();
                product.setQuantity(6L);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Alice: Optimistic locking failure", expected);
    }
    aliceLatch.countDown();
}
 
Example #15
Source File: OptimisticLockingOneRootOneVersionTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInTransaction(session -> {
            try {
                Product product = (Product) session.get(Product.class, 1L);
                loadProductsLatch.countDown();
                loadProductsLatch.await();
                aliceLatch.await();
                product.incrementLikes();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Bob: Optimistic locking failure", expected);
    }
}
 
Example #16
Source File: OptimisticLockingOneRootOneVersionTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInTransaction(session -> {
            try {
                Product product = (Product) session.get(Product.class, 1L);
                loadProductsLatch.countDown();
                loadProductsLatch.await();
                aliceLatch.await();
                product.setDescription("Plasma HDTV");
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Vlad: Optimistic locking failure", expected);
    }
}
 
Example #17
Source File: LockModePessimisticReadWriteIntegrationTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
private void testPessimisticLocking(ProductLockRequestCallable primaryLockRequestCallable, ProductLockRequestCallable secondaryLockRequestCallable) {
    doInTransaction(session -> {
        try {
            Product product = (Product) session.get(Product.class, 1L);
            primaryLockRequestCallable.lock(session, product);
            executeAsync(
                    () -> {
                        doInTransaction(_session -> {
                            Product _product = (Product) _session.get(Product.class, 1L);
                            secondaryLockRequestCallable.lock(_session, _product);
                        });
                    },
                    endLatch::countDown
            );
            sleep(WAIT_MILLIS);
        } catch (StaleObjectStateException e) {
            LOGGER.info("Optimistic locking failure: ", e);
        }
    });
    awaitOnLatch(endLatch);
}
 
Example #18
Source File: OptimisticLockingOneRootDirtyVersioningTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInTransaction(session -> {
            try {
                Product product = (Product) session.get(Product.class, 1L);
                loadProductsLatch.countDown();
                loadProductsLatch.await();
                product.setQuantity(6L);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Alice: Optimistic locking failure", expected);
    }
    aliceLatch.countDown();
}
 
Example #19
Source File: OptimisticLockingOneRootDirtyVersioningTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInTransaction(session -> {
            try {
                Product product = (Product) session.get(Product.class, 1L);
                loadProductsLatch.countDown();
                loadProductsLatch.await();
                aliceLatch.await();
                product.incrementLikes();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Bob: Optimistic locking failure", expected);
    }
}
 
Example #20
Source File: OptimisticLockingOneRootDirtyVersioningTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        doInTransaction(session -> {
            try {
                Product product = (Product) session.get(Product.class, 1L);
                loadProductsLatch.countDown();
                loadProductsLatch.await();
                aliceLatch.await();
                product.setDescription("Plasma HDTV");
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        });
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Vlad: Optimistic locking failure", expected);
    }
}
 
Example #21
Source File: LockModePessimisticForceIncrementTest.java    From hibernate-master-class with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcurrentPessimisticForceIncrementLockingFailFast() throws InterruptedException {
    LOGGER.info("Test Concurrent PESSIMISTIC_FORCE_INCREMENT Lock Mode fail fast");
    doInTransaction(session -> {
        try {
            Repository repository = (Repository) session.get(Repository.class, 1L);

            executeSync(() -> {
                doInTransaction(_session -> {
                    Repository _repository = (Repository) _session.get(Repository.class, 1L);
                    _session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(_repository);
                    Commit _commit = new Commit(_repository);
                    _commit.getChanges().add(new Change("index.html", "0a1,2..."));
                    _session.persist(_commit);
                    _session.flush();
                });
            });
            session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(repository);
            fail("Should have thrown StaleObjectStateException!");
        } catch (StaleObjectStateException expected) {
            LOGGER.info("Failure: ", expected);
        }
    });
}
 
Example #22
Source File: HibernateExceptionUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenUpdatingNonExistingObject_thenStaleStateException() {
    thrown.expect(isA(OptimisticLockException.class));
    thrown
        .expectMessage("Row was updated or deleted by another transaction");
    thrown.expectCause(isA(StaleObjectStateException.class));

    Session session = null;
    Transaction transaction = null;

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();

        Product product1 = new Product();
        product1.setId(15);
        product1.setName("Product1");
        session.update(product1);
        transaction.commit();
    } catch (Exception e) {
        rollbackTransactionQuietly(transaction);
        throw (e);
    } finally {
        closeSessionQuietly(session);
    }
}
 
Example #23
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) {
	if ( persister.isSelectBeforeUpdateRequired() ) {
		Object[] snapshot = session.getPersistenceContext()
				.getDatabaseSnapshot( id, persister );
		if ( snapshot == null ) {
			//do we even really need this? the update will fail anyway....
			if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatistics()
						.optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
		return snapshot;
	}
	// TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
	final EntityKey entityKey = session.generateEntityKey( id, persister );
	return session.getPersistenceContext().getCachedDatabaseSnapshot( entityKey );
}
 
Example #24
Source File: LockModeOptimisticForceIncrementTest.java    From hibernate-master-class with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentOptimisticForceIncrementLocking() throws InterruptedException {
    LOGGER.info("Test Concurrent OPTIMISTIC_FORCE_INCREMENT Lock Mode ");
    try {
        doInTransaction(session -> {
            Repository repository = (Repository) session.get(Repository.class, 1L);
            session.buildLockRequest(new LockOptions(LockMode.OPTIMISTIC_FORCE_INCREMENT)).lock(repository);

            executeSync(() -> {
                doInTransaction(_session -> {
                    Repository _repository = (Repository) _session.get(Repository.class, 1L);
                    _session.buildLockRequest(new LockOptions(LockMode.OPTIMISTIC_FORCE_INCREMENT)).lock(_repository);
                    Commit _commit = new Commit(_repository);
                    _commit.getChanges().add(new Change("index.html", "0a1,2..."));
                    _session.persist(_commit);
                });
            });

            Commit commit = new Commit(repository);
            commit.getChanges().add(new Change("README.txt", "0a1,5..."));
            commit.getChanges().add(new Change("web.xml", "17c17..."));
            session.persist(commit);
        });
        fail("Should have thrown StaleObjectStateException!");
    } catch (StaleObjectStateException expected) {
        LOGGER.info("Failure: ", expected);
    }
}
 
Example #25
Source File: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void checkVersion(
		SharedSessionContractImplementor session,
		ResultSet resultSet,
		EntityPersister persister,
		EntityAliases entityAliases,
		EntityKey entityKey,
		Object entityInstance) {
	final Object version = session.getPersistenceContext().getEntry( entityInstance ).getVersion();

	if ( version != null ) {
		//null version means the object is in the process of being loaded somewhere else in the ResultSet
		VersionType versionType = persister.getVersionType();
		final Object currentVersion;
		try {
			currentVersion = versionType.nullSafeGet(
					resultSet,
					entityAliases.getSuffixedVersionAliases(),
					session,
					null
			);
		}
		catch (SQLException e) {
			throw session.getFactory().getServiceRegistry().getService( JdbcServices.class ).getSqlExceptionHelper().convert(
					e,
					"Could not read version value from result set"
			);
		}

		if ( !versionType.isEqual( version, currentVersion ) ) {
			if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatistics().optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), entityKey.getIdentifier() );
		}
	}
}
 
Example #26
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check the version of the object in the <tt>ResultSet</tt> against
 * the object version in the session cache, throwing an exception
 * if the version numbers are different
 */
private void checkVersion(
		final int i,
		final Loadable persister,
		final Serializable id,
		final Object entity,
		final ResultSet rs,
		final SharedSessionContractImplementor session) throws HibernateException, SQLException {

	Object version = session.getPersistenceContext().getEntry( entity ).getVersion();

	if ( version != null ) { //null version means the object is in the process of being loaded somewhere else in the ResultSet
		final VersionType versionType = persister.getVersionType();
		final Object currentVersion = versionType.nullSafeGet(
				rs,
				getEntityAliases()[i].getSuffixedVersionAliases(),
				session,
				null
		);
		if ( !versionType.isEqual( version, currentVersion ) ) {
			if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatistics().optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
	}

}
 
Example #27
Source File: BaseHibernateManager.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void updateGradebook(final Gradebook gradebook) throws StaleObjectModificationException {
    final HibernateCallback hc = session -> {
        // Get the gradebook and selected mapping from persistence
        final Gradebook gradebookFromPersistence = (Gradebook)session.load(gradebook.getClass(), gradebook.getId());
        final GradeMapping mappingFromPersistence = gradebookFromPersistence.getSelectedGradeMapping();

        // If the mapping has changed, and there are explicitly entered
        // course grade records, disallow this update.
        if (!mappingFromPersistence.getId().equals(gradebook.getSelectedGradeMapping().getId())) {
            if(isExplicitlyEnteredCourseGradeRecords(gradebook.getId())) {
                throw new IllegalStateException("Selected grade mapping can not be changed, since explicit course grades exist.");
            }
        }

        // Evict the persisted objects from the session and update the gradebook
        // so the new grade mapping is used in the sort column update
        //session.evict(mappingFromPersistence);
        for (final Object element : gradebookFromPersistence.getGradeMappings()) {
            session.evict(element);
        }
        session.evict(gradebookFromPersistence);
        try {
            session.update(gradebook);
            session.flush();
        } catch (final StaleObjectStateException e) {
            throw new StaleObjectModificationException(e);
        }

        return null;
    };
    getHibernateTemplate().execute(hc);
}
 
Example #28
Source File: HSQLDialect.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session)
		throws StaleObjectStateException, JDBCException {
	if ( getLockMode().greaterThan( LockMode.READ ) ) {
		LOG.hsqldbSupportsOnlyReadCommittedIsolation();
	}
	super.lock( id, version, object, timeout, session );
}
 
Example #29
Source File: EntityOptimisticLockingOnComponentCollectionTest.java    From hibernate-master-class with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptimisticLocking() {
    try {
        simulateConcurrentTransactions(true);
    } catch (Exception e) {
        LOGGER.info("Expected", e);
        assertTrue(e instanceof StaleObjectStateException);
    }
}
 
Example #30
Source File: BaseHibernateManager.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void updateGradebook(final Gradebook gradebook) throws StaleObjectModificationException {
    final HibernateCallback hc = session -> {
        // Get the gradebook and selected mapping from persistence
        final Gradebook gradebookFromPersistence = (Gradebook)session.load(gradebook.getClass(), gradebook.getId());
        final GradeMapping mappingFromPersistence = gradebookFromPersistence.getSelectedGradeMapping();

        // If the mapping has changed, and there are explicitly entered
        // course grade records, disallow this update.
        if (!mappingFromPersistence.getId().equals(gradebook.getSelectedGradeMapping().getId())) {
            if(isExplicitlyEnteredCourseGradeRecords(gradebook.getId())) {
                throw new IllegalStateException("Selected grade mapping can not be changed, since explicit course grades exist.");
            }
        }

        // Evict the persisted objects from the session and update the gradebook
        // so the new grade mapping is used in the sort column update
        //session.evict(mappingFromPersistence);
        for (final Object element : gradebookFromPersistence.getGradeMappings()) {
            session.evict(element);
        }
        session.evict(gradebookFromPersistence);
        try {
            session.update(gradebook);
            session.flush();
        } catch (final StaleObjectStateException e) {
            throw new StaleObjectModificationException(e);
        }

        return null;
    };
    getHibernateTemplate().execute(hc);
}