FileDocCategorySizeDatePackage
Versioning.javaAPI DocHibernate 3.2.55249Tue Nov 21 16:28:30 GMT 2006org.hibernate.engine

Versioning

public final class Versioning extends Object
Utilities for dealing with optimisitic locking values.
author
Gavin King

Fields Summary
public static final int
OPTIMISTIC_LOCK_NONE
Apply no optimistic locking
public static final int
OPTIMISTIC_LOCK_VERSION
Apply optimisitc locking based on the defined version or timestamp property.
public static final int
OPTIMISTIC_LOCK_ALL
Apply optimisitc locking based on the a current vs. snapshot comparison of all properties.
public static final int
OPTIMISTIC_LOCK_DIRTY
Apply optimisitc locking based on the a current vs. snapshot comparison of dirty properties.
private static final Log
log
Constructors Summary
private Versioning()
Private constructor disallowing instantiation.


	    	 
	  
Methods Summary
public static java.lang.ObjectgetVersion(java.lang.Object[] fields, org.hibernate.persister.entity.EntityPersister persister)
Extract the optimisitc locking value out of the entity state snapshot.

param
fields The state snapshot
param
persister The entity persister
return
The extracted optimisitc locking value

		if ( !persister.isVersioned() ) {
			return null;
		}
		return fields[ persister.getVersionProperty() ];
	
public static java.lang.Objectincrement(java.lang.Object version, org.hibernate.type.VersionType versionType, SessionImplementor session)
Generate the next increment in the optimisitc locking value according the {@link VersionType} contract for the version property.

param
version The current version
param
versionType The version type
param
session The originating session
return
The incremented optimistic locking value.

		Object next = versionType.next( version, session );
		if ( log.isTraceEnabled() ) {
			log.trace(
					"Incrementing: " +
					versionType.toLoggableString( version, session.getFactory() ) +
					" to " +
					versionType.toLoggableString( next, session.getFactory() )
			);
		}
		return next;
	
public static booleanisVersionIncrementRequired(int[] dirtyProperties, boolean hasDirtyCollections, boolean[] propertyVersionability)
Do we need to increment the version number, given the dirty properties?

param
dirtyProperties The array of property indexes which were deemed dirty
param
hasDirtyCollections Were any collections found to be dirty (structurally changed)
param
propertyVersionability An array indicating versionability of each property.
return
True if a version increment is required; false otherwise.

		if ( hasDirtyCollections ) {
			return true;
		}
		for ( int i = 0; i < dirtyProperties.length; i++ ) {
			if ( propertyVersionability[ dirtyProperties[i] ] ) {
				return true;
			}
		}
	    return false;
	
private static java.lang.Objectseed(org.hibernate.type.VersionType versionType, SessionImplementor session)
Create an initial optimisitc locking value according the {@link VersionType} contract for the version property.

param
versionType The version type.
param
session The originating session
return
The initial optimisitc locking value

		Object seed = versionType.seed( session );
		if ( log.isTraceEnabled() ) log.trace("Seeding: " + seed);
		return seed;
	
public static booleanseedVersion(java.lang.Object[] fields, int versionProperty, org.hibernate.type.VersionType versionType, SessionImplementor session)
Create an initial optimisitc locking value according the {@link VersionType} contract for the version property if required and inject it into the snapshot state.

param
fields The current snapshot state
param
versionProperty The index of the version property
param
versionType The version type
param
session The orginating session
return
True if we injected a new version value into the fields array; false otherwise.

		Object initialVersion = fields[versionProperty];
		if (
			initialVersion==null ||
			// This next bit is to allow for both unsaved-value="negative"
			// and for "older" behavior where version number did not get
			// seeded if it was already set in the object
			// TODO: shift it into unsaved-value strategy
			( (initialVersion instanceof Number) && ( (Number) initialVersion ).longValue()<0 )
		) {
			fields[versionProperty] = seed( versionType, session );
			return true;
		}
		else {
			if ( log.isTraceEnabled() ) {
				log.trace( "using initial version: " + initialVersion );
			}
			return false;
		}
	
public static voidsetVersion(java.lang.Object[] fields, java.lang.Object version, org.hibernate.persister.entity.EntityPersister persister)
Inject the optimisitc locking value into the entity state snapshot.

param
fields The state snapshot
param
version The optimisitc locking value
param
persister The entity persister

		if ( !persister.isVersioned() ) {
			return;
		}
		fields[ persister.getVersionProperty() ] = version;