hibernate/hibernate-orm · error · HibernateException

Force-increment lock not supported for '@Version' generator

Error message

Force-increment lock not supported for '@Version' generator

What it means

Mapping/configuration guard during force-increment version locking: isVersionGeneratedOnExecution() is true and the version generator is not one of the special cases that can be treated as a BeforeExecutionGenerator, so the version cannot be incremented from the application side as a force-increment lock requires. The faulty configuration is an execution-generated @Version value (e.g. DB default/trigger) on an entity being locked with OPTIMISTIC_FORCE_INCREMENT.

Solutions

  1. Replace the version value generator with a plain @Version mapping to use force-increment locking.
  2. Use PESSIMISTIC locking instead of OPTIMISTIC_FORCE_INCREMENT.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown when a force-increment optimistic lock is requested on an entity whose @Version value is database-generated.

Common situations: See trigger scenarios.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/3ef0ea87d64fb12e. Report an issue: GitHub.

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java:2300

			// Special case to accommodate the fact that we don't yet
			// allow OnExecutionGenerators with force-increment locking.
			// When possible, falls back to treating the generator as a
			// BeforeExecutionGenerator. In particular, this works for
			// CurrentTimestampGeneration. But it requires an additional
			// request to the database to generate the timestamp. This
			// solution is neither particularly elegant nor efficient.
			return generator;
		}
		else if ( isVersionGeneratedOnExecution() ) {
			// TODO: Ideally, we would allow this case, producing an
			//       UPDATE statement which sets the version column.
			//       Then we could remove the previous special case.
			throw new HibernateException( "Force-increment lock not supported for '@Version' property with OnExecutionGenerator" );
		}
		else {
			final var generator = getVersionGenerator();
			if ( !generator.generatesOnForceIncrement() ) {
				throw new HibernateException( "Force-increment lock not supported for '@Version' generator" );
			}
			return generator;
		}
	}

	/**
	 * Retrieve the version number
	 */
	@Override
	public Object getCurrentVersion(Object id, SharedSessionContractImplementor session) throws HibernateException {

		if ( CORE_LOGGER.isTraceEnabled() ) {
			CORE_LOGGER.readingEntityVersion( infoString( this, id, getFactory() ) );
		}
		final String versionSelectString = getVersionSelectString();
		try {
			final var jdbcCoordinator = session.getJdbcCoordinator();
			final var statement = jdbcCoordinator.getStatementPreparer().prepareStatement( versionSelectString );

View on GitHub (pinned to fad1729dce)