1 package org.hibernate; 3 4 import net.sf.cglib.transform.impl.InterceptFieldEnabled; 5 import org.hibernate.collection.PersistentCollection; 6 import org.hibernate.engine.HibernateIterator; 7 import org.hibernate.intercept.FieldInterceptor; 8 import org.hibernate.lob.BlobImpl; 9 import org.hibernate.lob.ClobImpl; 10 import org.hibernate.lob.SerializableBlob; 11 import org.hibernate.lob.SerializableClob; 12 import org.hibernate.proxy.HibernateProxy; 13 import org.hibernate.type.AnyType; 14 import org.hibernate.type.BigDecimalType; 15 import org.hibernate.type.BigIntegerType; 16 import org.hibernate.type.BinaryType; 17 import org.hibernate.type.BlobType; 18 import org.hibernate.type.BooleanType; 19 import org.hibernate.type.ByteType; 20 import org.hibernate.type.CalendarDateType; 21 import org.hibernate.type.CalendarType; 22 import org.hibernate.type.CharacterType; 23 import org.hibernate.type.ClassType; 24 import org.hibernate.type.ClobType; 25 import org.hibernate.type.CompositeCustomType; 26 import org.hibernate.type.CurrencyType; 27 import org.hibernate.type.CustomType; 28 import org.hibernate.type.DateType; 29 import org.hibernate.type.DoubleType; 30 import org.hibernate.type.FloatType; 31 import org.hibernate.type.IntegerType; 32 import org.hibernate.type.LocaleType; 33 import org.hibernate.type.LongType; 34 import org.hibernate.type.ManyToOneType; 35 import org.hibernate.type.NullableType; 36 import org.hibernate.type.SerializableType; 37 import org.hibernate.type.ShortType; 38 import org.hibernate.type.StringType; 39 import org.hibernate.type.TextType; 40 import org.hibernate.type.TimeType; 41 import org.hibernate.type.TimeZoneType; 42 import org.hibernate.type.TimestampType; 43 import org.hibernate.type.TrueFalseType; 44 import org.hibernate.type.Type; 45 import org.hibernate.type.YesNoType; 46 import org.hibernate.usertype.CompositeUserType; 47 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.io.Reader ; 51 import java.io.Serializable ; 52 import java.sql.Blob ; 53 import java.sql.Clob ; 54 import java.util.Iterator ; 55 import java.util.Properties ; 56 57 70 71 public final class Hibernate { 72 73 76 public static final NullableType LONG = new LongType(); 77 80 public static final NullableType SHORT = new ShortType(); 81 84 public static final NullableType INTEGER = new IntegerType(); 85 88 public static final NullableType BYTE = new ByteType(); 89 92 public static final NullableType FLOAT = new FloatType(); 93 96 public static final NullableType DOUBLE = new DoubleType(); 97 100 public static final NullableType CHARACTER = new CharacterType(); 101 104 public static final NullableType STRING = new StringType(); 105 108 public static final NullableType TIME = new TimeType(); 109 112 public static final NullableType DATE = new DateType(); 113 116 public static final NullableType TIMESTAMP = new TimestampType(); 117 120 public static final NullableType BOOLEAN = new BooleanType(); 121 124 public static final NullableType TRUE_FALSE = new TrueFalseType(); 125 128 public static final NullableType YES_NO = new YesNoType(); 129 132 public static final NullableType BIG_DECIMAL = new BigDecimalType(); 133 136 public static final NullableType BIG_INTEGER = new BigIntegerType(); 137 140 public static final NullableType BINARY = new BinaryType(); 141 144 public static final NullableType TEXT = new TextType(); 145 148 public static final Type BLOB = new BlobType(); 149 152 public static final Type CLOB = new ClobType(); 153 156 public static final NullableType CALENDAR = new CalendarType(); 157 160 public static final NullableType CALENDAR_DATE = new CalendarDateType(); 161 164 public static final NullableType LOCALE = new LocaleType(); 165 168 public static final NullableType CURRENCY = new CurrencyType(); 169 172 public static final NullableType TIMEZONE = new TimeZoneType(); 173 176 public static final NullableType CLASS = new ClassType(); 177 180 public static final NullableType SERIALIZABLE = new SerializableType( Serializable .class ); 181 184 public static final Type OBJECT = new AnyType(); 185 186 187 190 private Hibernate() { 191 throw new UnsupportedOperationException (); 192 } 193 194 197 public static Type serializable(Class serializableClass) { 198 return new SerializableType( serializableClass ); 199 } 200 201 208 public static Type any(Type metaType, Type identifierType) { 209 return new AnyType( metaType, identifierType ); 210 } 211 212 217 public static Type entity(Class persistentClass) { 218 return new ManyToOneType( persistentClass.getName() ); 220 } 221 222 227 public static Type entity(String entityName) { 228 return new ManyToOneType( entityName ); 230 } 231 232 237 public static Type custom(Class userTypeClass) throws HibernateException { 238 return custom( userTypeClass, null ); 239 } 240 241 249 public static Type custom(Class userTypeClass, String [] parameterNames, String [] parameterValues) 250 throws HibernateException { 251 Properties parameters = new Properties (); 252 for ( int i = 0; i < parameterNames.length; i++ ) { 253 parameters.setProperty( parameterNames[i], parameterValues[i] ); 254 } 255 return custom( userTypeClass, parameters ); 256 } 257 258 264 public static Type custom(Class userTypeClass, Properties parameters) 265 throws HibernateException { 266 if ( CompositeUserType.class.isAssignableFrom( userTypeClass ) ) { 267 CompositeCustomType type = new CompositeCustomType( userTypeClass, parameters ); 268 return type; 269 } 270 else { 271 CustomType type = new CustomType( userTypeClass, parameters ); 272 return type; 273 } 274 } 275 276 285 public static void initialize(Object proxy) throws HibernateException { 286 if ( proxy == null ) { 287 return; 288 } 289 else if ( proxy instanceof HibernateProxy ) { 290 ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize(); 291 } 292 else if ( proxy instanceof PersistentCollection ) { 293 ( ( PersistentCollection ) proxy ).forceInitialization(); 294 } 295 } 296 297 303 public static boolean isInitialized(Object proxy) { 304 if ( proxy instanceof HibernateProxy ) { 305 return !( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isUninitialized(); 306 } 307 else if ( proxy instanceof PersistentCollection ) { 308 return ( ( PersistentCollection ) proxy ).wasInitialized(); 309 } 310 else { 311 return true; 312 } 313 } 314 315 323 public static Class getClass(Object proxy) { 324 if ( proxy instanceof HibernateProxy ) { 325 return ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer() 326 .getImplementation() 327 .getClass(); 328 } 329 else { 330 return proxy.getClass(); 331 } 332 } 333 334 340 public static Blob createBlob(byte[] bytes) { 341 return new SerializableBlob( new BlobImpl( bytes ) ); 342 } 343 344 351 public static Blob createBlob(InputStream stream, int length) { 352 return new SerializableBlob( new BlobImpl( stream, length ) ); 353 } 354 355 362 public static Blob createBlob(InputStream stream) throws IOException { 363 return new SerializableBlob( new BlobImpl( stream, stream.available() ) ); 364 } 365 366 371 public static Clob createClob(String string) { 372 return new SerializableClob( new ClobImpl( string ) ); 373 } 374 375 381 public static Clob createClob(Reader reader, int length) { 382 return new SerializableClob( new ClobImpl( reader, length ) ); 383 } 384 385 394 public static void close(Iterator iterator) throws HibernateException { 395 if ( iterator instanceof HibernateIterator ) { 396 ( ( HibernateIterator ) iterator ).close(); 397 } 398 else { 399 throw new IllegalArgumentException ( "not a Hibernate iterator" ); 400 } 401 } 402 403 411 public static boolean isPropertyInitialized(Object entity, String propertyName) { 412 if ( entity instanceof InterceptFieldEnabled ) { 413 FieldInterceptor fieldInterceptor = FieldInterceptor.getFieldInterceptor(entity); 414 return fieldInterceptor != null && fieldInterceptor.isInitialized( propertyName ); 415 } 416 return true; 417 } 418 419 } 420 421 422 423 424 425 426 | Popular Tags |