1 package org.hibernate.engine; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 7 import org.hibernate.HibernateException; 8 import org.hibernate.persister.entity.EntityPersister; 9 import org.hibernate.type.VersionType; 10 11 15 public final class Versioning { 16 17 private Versioning() {} 18 19 private static final Log log = LogFactory.getLog(Versioning.class); 20 21 24 public static Object increment(Object version, VersionType versionType) { 25 Object next = versionType.next(version); 26 if ( log.isTraceEnabled() ) log.trace("Incrementing: " + version + " to " + next); 27 return next; 28 } 29 30 33 private static Object seed(VersionType versionType) { 34 Object seed = versionType.seed(); 35 if ( log.isTraceEnabled() ) log.trace("Seeding: " + seed); 36 return seed; 37 } 38 39 42 public static boolean seedVersion(Object [] fields, int versionProperty, VersionType versionType) { 43 Object initialVersion = fields[versionProperty]; 44 if ( 45 initialVersion==null || 46 ( (initialVersion instanceof Number ) && ( (Number ) initialVersion ).longValue()<0 ) 51 ) { 52 fields[versionProperty] = seed(versionType); 53 return true; 54 } 55 else { 56 if ( log.isTraceEnabled() ) log.trace( "using initial version: " + initialVersion ); 57 return false; 58 } 59 } 60 61 private static Object getVersion(Object [] fields, int versionProperty) { 62 return fields[versionProperty]; 63 } 64 65 private static void setVersion(Object [] fields, Object version, int versionProperty) { 66 fields[versionProperty] = version; 67 } 68 69 72 public static void setVersion(Object [] fields, Object version, EntityPersister persister) { 73 setVersion( fields, version, persister.getVersionProperty() ); 74 } 75 76 79 public static Object getVersion(Object [] fields, EntityPersister persister) throws HibernateException { 80 return persister.isVersioned() ? getVersion( fields, persister.getVersionProperty() ) : null; 81 } 82 83 86 public static boolean isVersionIncrementRequired( 87 final int[] dirtyProperties, 88 final boolean hasDirtyCollections, 89 final boolean[] propertyVersionability 90 ) { 91 if (hasDirtyCollections) return true; 92 for ( int i=0; i<dirtyProperties.length; i++) { 93 if ( propertyVersionability[ dirtyProperties[i] ] ) return true; 94 } 95 return false; 96 } 97 98 public static final int OPTIMISTIC_LOCK_NONE = -1; 99 public static final int OPTIMISTIC_LOCK_ALL = 2; 100 public static final int OPTIMISTIC_LOCK_DIRTY = 1; 101 public static final int OPTIMISTIC_LOCK_VERSION = 0; 102 103 } 104 105 106 107 108 109 110 | Popular Tags |