KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > Hibernate


1 //$Id: Hibernate.java,v 1.17 2005/06/19 07:13:35 oneovthafew Exp $
2
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 JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.Reader JavaDoc;
51 import java.io.Serializable JavaDoc;
52 import java.sql.Blob JavaDoc;
53 import java.sql.Clob JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.Properties JavaDoc;
56
57 /**
58  * <ul>
59  * <li>Provides access to the full range of Hibernate built-in types. <tt>Type</tt>
60  * instances may be used to bind values to query parameters.
61  * <li>A factory for new <tt>Blob</tt>s and <tt>Clob</tt>s.
62  * <li>Defines static methods for manipulation of proxies.
63  * </ul>
64  *
65  * @author Gavin King
66  * @see java.sql.Clob
67  * @see java.sql.Blob
68  * @see org.hibernate.type.Type
69  */

70
71 public final class Hibernate {
72
73     /**
74      * Hibernate <tt>long</tt> type.
75      */

76     public static final NullableType LONG = new LongType();
77     /**
78      * Hibernate <tt>short</tt> type.
79      */

80     public static final NullableType SHORT = new ShortType();
81     /**
82      * Hibernate <tt>integer</tt> type.
83      */

84     public static final NullableType INTEGER = new IntegerType();
85     /**
86      * Hibernate <tt>byte</tt> type.
87      */

88     public static final NullableType BYTE = new ByteType();
89     /**
90      * Hibernate <tt>float</tt> type.
91      */

92     public static final NullableType FLOAT = new FloatType();
93     /**
94      * Hibernate <tt>double</tt> type.
95      */

96     public static final NullableType DOUBLE = new DoubleType();
97     /**
98      * Hibernate <tt>character</tt> type.
99      */

100     public static final NullableType CHARACTER = new CharacterType();
101     /**
102      * Hibernate <tt>string</tt> type.
103      */

104     public static final NullableType STRING = new StringType();
105     /**
106      * Hibernate <tt>time</tt> type.
107      */

108     public static final NullableType TIME = new TimeType();
109     /**
110      * Hibernate <tt>date</tt> type.
111      */

112     public static final NullableType DATE = new DateType();
113     /**
114      * Hibernate <tt>timestamp</tt> type.
115      */

116     public static final NullableType TIMESTAMP = new TimestampType();
117     /**
118      * Hibernate <tt>boolean</tt> type.
119      */

120     public static final NullableType BOOLEAN = new BooleanType();
121     /**
122      * Hibernate <tt>true_false</tt> type.
123      */

124     public static final NullableType TRUE_FALSE = new TrueFalseType();
125     /**
126      * Hibernate <tt>yes_no</tt> type.
127      */

128     public static final NullableType YES_NO = new YesNoType();
129     /**
130      * Hibernate <tt>big_decimal</tt> type.
131      */

132     public static final NullableType BIG_DECIMAL = new BigDecimalType();
133     /**
134      * Hibernate <tt>big_integer</tt> type.
135      */

136     public static final NullableType BIG_INTEGER = new BigIntegerType();
137     /**
138      * Hibernate <tt>binary</tt> type.
139      */

140     public static final NullableType BINARY = new BinaryType();
141     /**
142      * Hibernate <tt>text</tt> type.
143      */

144     public static final NullableType TEXT = new TextType();
145     /**
146      * Hibernate <tt>blob</tt> type.
147      */

148     public static final Type BLOB = new BlobType();
149     /**
150      * Hibernate <tt>clob</tt> type.
151      */

152     public static final Type CLOB = new ClobType();
153     /**
154      * Hibernate <tt>calendar</tt> type.
155      */

156     public static final NullableType CALENDAR = new CalendarType();
157     /**
158      * Hibernate <tt>calendar_date</tt> type.
159      */

160     public static final NullableType CALENDAR_DATE = new CalendarDateType();
161     /**
162      * Hibernate <tt>locale</tt> type.
163      */

164     public static final NullableType LOCALE = new LocaleType();
165     /**
166      * Hibernate <tt>currency</tt> type.
167      */

168     public static final NullableType CURRENCY = new CurrencyType();
169     /**
170      * Hibernate <tt>timezone</tt> type.
171      */

172     public static final NullableType TIMEZONE = new TimeZoneType();
173     /**
174      * Hibernate <tt>class</tt> type.
175      */

176     public static final NullableType CLASS = new ClassType();
177     /**
178      * Hibernate <tt>serializable</tt> type.
179      */

180     public static final NullableType SERIALIZABLE = new SerializableType( Serializable JavaDoc.class );
181     /**
182      * Hibernate <tt>object</tt> type.
183      */

184     public static final Type OBJECT = new AnyType();
185
186
187     /**
188      * Cannot be instantiated.
189      */

190     private Hibernate() {
191         throw new UnsupportedOperationException JavaDoc();
192     }
193
194     /**
195      * A Hibernate <tt>serializable</tt> type.
196      */

197     public static Type serializable(Class JavaDoc serializableClass) {
198         return new SerializableType( serializableClass );
199     }
200
201     /**
202      * A Hibernate <tt>any</tt> type.
203      *
204      * @param metaType a type mapping <tt>java.lang.Class</tt> to a single column
205      * @param identifierType the entity identifier type
206      * @return the Type
207      */

208     public static Type any(Type metaType, Type identifierType) {
209         return new AnyType( metaType, identifierType );
210     }
211
212     /**
213      * A Hibernate persistent object (entity) type.
214      *
215      * @param persistentClass a mapped entity class
216      */

217     public static Type entity(Class JavaDoc persistentClass) {
218         // not really a many-to-one association *necessarily*
219
return new ManyToOneType( persistentClass.getName() );
220     }
221
222     /**
223      * A Hibernate persistent object (entity) type.
224      *
225      * @param entityName a mapped entity class
226      */

227     public static Type entity(String JavaDoc entityName) {
228         // not really a many-to-one association *necessarily*
229
return new ManyToOneType( entityName );
230     }
231
232     /**
233      * A Hibernate custom type.
234      *
235      * @param userTypeClass a class that implements <tt>UserType</tt>
236      */

237     public static Type custom(Class JavaDoc userTypeClass) throws HibernateException {
238         return custom( userTypeClass, null );
239     }
240
241     /**
242      * A Hibernate parameterizable custom type.
243      *
244      * @param userTypeClass a class that implements <tt>UserType and ParameterizableType</tt>
245      * @param parameterNames the names of the parameters passed to the type
246      * @param parameterValues the values of the parameters passed to the type. They must match
247      * up with the order and length of the parameterNames array.
248      */

249     public static Type custom(Class JavaDoc userTypeClass, String JavaDoc[] parameterNames, String JavaDoc[] parameterValues)
250             throws HibernateException {
251         Properties JavaDoc parameters = new Properties JavaDoc();
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     /**
259      * A Hibernate parameterizable custom type.
260      *
261      * @param userTypeClass a class that implements <tt>UserType and ParameterizableType</tt>
262      * @param parameters the parameters as a collection of name/value pairs
263      */

264     public static Type custom(Class JavaDoc userTypeClass, Properties JavaDoc 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     /**
277      * Force initialization of a proxy or persistent collection.
278      * <p/>
279      * Note: This only ensures intialization of a proxy object or collection;
280      * it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
281      *
282      * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
283      * @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed
284      */

285     public static void initialize(Object JavaDoc 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     /**
298      * Check if the proxy or persistent collection is initialized.
299      *
300      * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
301      * @return true if the argument is already initialized, or is not a proxy or collection
302      */

303     public static boolean isInitialized(Object JavaDoc 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     /**
316      * Get the true, underlying class of a proxied persistent class. This operation
317      * will initialize a proxy by side-effect.
318      *
319      * @param proxy a persistable object or proxy
320      * @return the true class of the instance
321      * @throws HibernateException
322      */

323     public static Class JavaDoc getClass(Object JavaDoc 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     /**
335      * Create a new <tt>Blob</tt>. The returned object will be initially immutable.
336      *
337      * @param bytes a byte array
338      * @return the Blob
339      */

340     public static Blob createBlob(byte[] bytes) {
341         return new SerializableBlob( new BlobImpl( bytes ) );
342     }
343
344     /**
345      * Create a new <tt>Blob</tt>. The returned object will be initially immutable.
346      *
347      * @param stream a binary stream
348      * @param length the number of bytes in the stream
349      * @return the Blob
350      */

351     public static Blob createBlob(InputStream JavaDoc stream, int length) {
352         return new SerializableBlob( new BlobImpl( stream, length ) );
353     }
354
355     /**
356      * Create a new <tt>Blob</tt>. The returned object will be initially immutable.
357      *
358      * @param stream a binary stream
359      * @return the Blob
360      * @throws IOException
361      */

362     public static Blob createBlob(InputStream JavaDoc stream) throws IOException JavaDoc {
363         return new SerializableBlob( new BlobImpl( stream, stream.available() ) );
364     }
365
366     /**
367      * Create a new <tt>Clob</tt>. The returned object will be initially immutable.
368      *
369      * @param string a <tt>String</tt>
370      */

371     public static Clob createClob(String JavaDoc string) {
372         return new SerializableClob( new ClobImpl( string ) );
373     }
374
375     /**
376      * Create a new <tt>Clob</tt>. The returned object will be initially immutable.
377      *
378      * @param reader a character stream
379      * @param length the number of characters in the stream
380      */

381     public static Clob createClob(Reader JavaDoc reader, int length) {
382         return new SerializableClob( new ClobImpl( reader, length ) );
383     }
384
385     /**
386      * Close an <tt>Iterator</tt> created by <tt>iterate()</tt> immediately,
387      * instead of waiting until the session is closed or disconnected.
388      *
389      * @param iterator an <tt>Iterator</tt> created by <tt>iterate()</tt>
390      * @throws HibernateException
391      * @see org.hibernate.classic.Session#iterate(java.lang.String)
392      * @see Query#iterate()
393      */

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 JavaDoc( "not a Hibernate iterator" );
400         }
401     }
402
403     /**
404      * Check if the property is initialized.
405      * Note: if you test on a invalid property the method will always return true.
406      * No existing-checking is performed.
407      *
408      * @param entity a persistable object
409      * @return true if the argument is not listed as uninitialized
410      */

411     public static boolean isPropertyInitialized(Object JavaDoc entity, String JavaDoc 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