Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
33 | PointedEar | 1 | /* |
2 | * Hibernate, Relational Persistence for Idiomatic Java |
||
3 | * |
||
4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as |
||
5 | * indicated by the @author tags or express copyright attribution |
||
6 | * statements applied by the authors. All third-party contributions are |
||
7 | * distributed under license by Red Hat Middleware LLC. |
||
8 | * |
||
9 | * This copyrighted material is made available to anyone wishing to use, modify, |
||
10 | * copy, or redistribute it subject to the terms and conditions of the GNU |
||
11 | * Lesser General Public License, as published by the Free Software Foundation. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
||
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
||
16 | * for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Lesser General Public License |
||
19 | * along with this distribution; if not, write to: |
||
20 | * Free Software Foundation, Inc. |
||
21 | * 51 Franklin Street, Fifth Floor |
||
22 | * Boston, MA 02110-1301 USA |
||
23 | * |
||
24 | */ |
||
25 | package org.hibernate; |
||
26 | |||
27 | import java.io.IOException; |
||
28 | import java.io.InputStream; |
||
29 | import java.io.Reader; |
||
30 | import java.io.Serializable; |
||
31 | import java.sql.Blob; |
||
32 | import java.sql.Clob; |
||
33 | import java.util.Iterator; |
||
34 | import java.util.Properties; |
||
35 | |||
36 | import org.hibernate.collection.PersistentCollection; |
||
37 | import org.hibernate.engine.HibernateIterator; |
||
38 | import org.hibernate.intercept.FieldInterceptionHelper; |
||
39 | import org.hibernate.intercept.FieldInterceptor; |
||
40 | import org.hibernate.lob.BlobImpl; |
||
41 | import org.hibernate.lob.ClobImpl; |
||
42 | import org.hibernate.lob.SerializableBlob; |
||
43 | import org.hibernate.lob.SerializableClob; |
||
44 | import org.hibernate.proxy.HibernateProxy; |
||
45 | import org.hibernate.proxy.LazyInitializer; |
||
46 | import org.hibernate.type.AnyType; |
||
47 | import org.hibernate.type.BigDecimalType; |
||
48 | import org.hibernate.type.BigIntegerType; |
||
49 | import org.hibernate.type.BinaryType; |
||
50 | import org.hibernate.type.BlobType; |
||
51 | import org.hibernate.type.BooleanType; |
||
52 | import org.hibernate.type.ByteType; |
||
53 | import org.hibernate.type.CalendarDateType; |
||
54 | import org.hibernate.type.CalendarType; |
||
55 | import org.hibernate.type.CharacterType; |
||
56 | import org.hibernate.type.ClassType; |
||
57 | import org.hibernate.type.ClobType; |
||
58 | import org.hibernate.type.CompositeCustomType; |
||
59 | import org.hibernate.type.CurrencyType; |
||
60 | import org.hibernate.type.CustomType; |
||
61 | import org.hibernate.type.DateType; |
||
62 | import org.hibernate.type.DoubleType; |
||
63 | import org.hibernate.type.FloatType; |
||
64 | import org.hibernate.type.IntegerType; |
||
65 | import org.hibernate.type.LocaleType; |
||
66 | import org.hibernate.type.LongType; |
||
67 | import org.hibernate.type.ManyToOneType; |
||
68 | import org.hibernate.type.NullableType; |
||
69 | import org.hibernate.type.SerializableType; |
||
70 | import org.hibernate.type.ShortType; |
||
71 | import org.hibernate.type.StringType; |
||
72 | import org.hibernate.type.TextType; |
||
73 | import org.hibernate.type.TimeType; |
||
74 | import org.hibernate.type.TimeZoneType; |
||
75 | import org.hibernate.type.TimestampType; |
||
76 | import org.hibernate.type.TrueFalseType; |
||
77 | import org.hibernate.type.Type; |
||
78 | import org.hibernate.type.YesNoType; |
||
79 | import org.hibernate.type.CharArrayType; |
||
80 | import org.hibernate.type.WrapperBinaryType; |
||
81 | import org.hibernate.type.CharacterArrayType; |
||
82 | import org.hibernate.usertype.CompositeUserType; |
||
83 | |||
84 | /** |
||
85 | * <ul> |
||
86 | * <li>Provides access to the full range of Hibernate built-in types. <tt>Type</tt> |
||
87 | * instances may be used to bind values to query parameters. |
||
88 | * <li>A factory for new <tt>Blob</tt>s and <tt>Clob</tt>s. |
||
89 | * <li>Defines static methods for manipulation of proxies. |
||
90 | * </ul> |
||
91 | * |
||
92 | * @author Gavin King |
||
93 | * @see java.sql.Clob |
||
94 | * @see java.sql.Blob |
||
95 | * @see org.hibernate.type.Type |
||
96 | */ |
||
97 | |||
98 | public final class Hibernate { |
||
99 | |||
100 | /** |
||
101 | * Hibernate <tt>long</tt> type. |
||
102 | */ |
||
103 | public static final NullableType LONG = new LongType(); |
||
104 | /** |
||
105 | * Hibernate <tt>short</tt> type. |
||
106 | */ |
||
107 | public static final NullableType SHORT = new ShortType(); |
||
108 | /** |
||
109 | * Hibernate <tt>integer</tt> type. |
||
110 | */ |
||
111 | public static final NullableType INTEGER = new IntegerType(); |
||
112 | /** |
||
113 | * Hibernate <tt>byte</tt> type. |
||
114 | */ |
||
115 | public static final NullableType BYTE = new ByteType(); |
||
116 | /** |
||
117 | * Hibernate <tt>float</tt> type. |
||
118 | */ |
||
119 | public static final NullableType FLOAT = new FloatType(); |
||
120 | /** |
||
121 | * Hibernate <tt>double</tt> type. |
||
122 | */ |
||
123 | public static final NullableType DOUBLE = new DoubleType(); |
||
124 | /** |
||
125 | * Hibernate <tt>character</tt> type. |
||
126 | */ |
||
127 | public static final NullableType CHARACTER = new CharacterType(); |
||
128 | /** |
||
129 | * Hibernate <tt>string</tt> type. |
||
130 | */ |
||
131 | public static final NullableType STRING = new StringType(); |
||
132 | /** |
||
133 | * Hibernate <tt>time</tt> type. |
||
134 | */ |
||
135 | public static final NullableType TIME = new TimeType(); |
||
136 | /** |
||
137 | * Hibernate <tt>date</tt> type. |
||
138 | */ |
||
139 | public static final NullableType DATE = new DateType(); |
||
140 | /** |
||
141 | * Hibernate <tt>timestamp</tt> type. |
||
142 | */ |
||
143 | public static final NullableType TIMESTAMP = new TimestampType(); |
||
144 | /** |
||
145 | * Hibernate <tt>boolean</tt> type. |
||
146 | */ |
||
147 | public static final NullableType BOOLEAN = new BooleanType(); |
||
148 | /** |
||
149 | * Hibernate <tt>true_false</tt> type. |
||
150 | */ |
||
151 | public static final NullableType TRUE_FALSE = new TrueFalseType(); |
||
152 | /** |
||
153 | * Hibernate <tt>yes_no</tt> type. |
||
154 | */ |
||
155 | public static final NullableType YES_NO = new YesNoType(); |
||
156 | /** |
||
157 | * Hibernate <tt>big_decimal</tt> type. |
||
158 | */ |
||
159 | public static final NullableType BIG_DECIMAL = new BigDecimalType(); |
||
160 | /** |
||
161 | * Hibernate <tt>big_integer</tt> type. |
||
162 | */ |
||
163 | public static final NullableType BIG_INTEGER = new BigIntegerType(); |
||
164 | /** |
||
165 | * Hibernate <tt>binary</tt> type. |
||
166 | */ |
||
167 | public static final NullableType BINARY = new BinaryType(); |
||
168 | /** |
||
169 | * Hibernate <tt>wrapper-binary</tt> type. |
||
170 | */ |
||
171 | public static final NullableType WRAPPER_BINARY = new WrapperBinaryType(); |
||
172 | /** |
||
173 | * Hibernate char[] type. |
||
174 | */ |
||
175 | public static final NullableType CHAR_ARRAY = new CharArrayType(); |
||
176 | /** |
||
177 | * Hibernate Character[] type. |
||
178 | */ |
||
179 | public static final NullableType CHARACTER_ARRAY = new CharacterArrayType(); |
||
180 | /** |
||
181 | * Hibernate <tt>text</tt> type. |
||
182 | */ |
||
183 | public static final NullableType TEXT = new TextType(); |
||
184 | /** |
||
185 | * Hibernate <tt>blob</tt> type. |
||
186 | */ |
||
187 | public static final Type BLOB = new BlobType(); |
||
188 | /** |
||
189 | * Hibernate <tt>clob</tt> type. |
||
190 | */ |
||
191 | public static final Type CLOB = new ClobType(); |
||
192 | /** |
||
193 | * Hibernate <tt>calendar</tt> type. |
||
194 | */ |
||
195 | public static final NullableType CALENDAR = new CalendarType(); |
||
196 | /** |
||
197 | * Hibernate <tt>calendar_date</tt> type. |
||
198 | */ |
||
199 | public static final NullableType CALENDAR_DATE = new CalendarDateType(); |
||
200 | /** |
||
201 | * Hibernate <tt>locale</tt> type. |
||
202 | */ |
||
203 | public static final NullableType LOCALE = new LocaleType(); |
||
204 | /** |
||
205 | * Hibernate <tt>currency</tt> type. |
||
206 | */ |
||
207 | public static final NullableType CURRENCY = new CurrencyType(); |
||
208 | /** |
||
209 | * Hibernate <tt>timezone</tt> type. |
||
210 | */ |
||
211 | public static final NullableType TIMEZONE = new TimeZoneType(); |
||
212 | /** |
||
213 | * Hibernate <tt>class</tt> type. |
||
214 | */ |
||
215 | public static final NullableType CLASS = new ClassType(); |
||
216 | /** |
||
217 | * Hibernate <tt>serializable</tt> type. |
||
218 | */ |
||
219 | public static final NullableType SERIALIZABLE = new SerializableType( Serializable.class ); |
||
220 | /** |
||
221 | * Hibernate <tt>object</tt> type. |
||
222 | */ |
||
223 | public static final Type OBJECT = new AnyType(); |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Cannot be instantiated. |
||
228 | */ |
||
229 | private Hibernate() { |
||
230 | throw new UnsupportedOperationException(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * A Hibernate <tt>serializable</tt> type. |
||
235 | */ |
||
236 | public static Type serializable(Class serializableClass) { |
||
237 | return new SerializableType( serializableClass ); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * A Hibernate <tt>any</tt> type. |
||
242 | * |
||
243 | * @param metaType a type mapping <tt>java.lang.Class</tt> to a single column |
||
244 | * @param identifierType the entity identifier type |
||
245 | * @return the Type |
||
246 | */ |
||
247 | public static Type any(Type metaType, Type identifierType) { |
||
248 | return new AnyType( metaType, identifierType ); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * A Hibernate persistent object (entity) type. |
||
253 | * |
||
254 | * @param persistentClass a mapped entity class |
||
255 | */ |
||
256 | public static Type entity(Class persistentClass) { |
||
257 | // not really a many-to-one association *necessarily* |
||
258 | return new ManyToOneType( persistentClass.getName() ); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * A Hibernate persistent object (entity) type. |
||
263 | * |
||
264 | * @param entityName a mapped entity class |
||
265 | */ |
||
266 | public static Type entity(String entityName) { |
||
267 | // not really a many-to-one association *necessarily* |
||
268 | return new ManyToOneType( entityName ); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * A Hibernate custom type. |
||
273 | * |
||
274 | * @param userTypeClass a class that implements <tt>UserType</tt> |
||
275 | */ |
||
276 | public static Type custom(Class userTypeClass) throws HibernateException { |
||
277 | return custom( userTypeClass, null ); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * A Hibernate parameterizable custom type. |
||
282 | * |
||
283 | * @param userTypeClass a class that implements <tt>UserType and ParameterizableType</tt> |
||
284 | * @param parameterNames the names of the parameters passed to the type |
||
285 | * @param parameterValues the values of the parameters passed to the type. They must match |
||
286 | * up with the order and length of the parameterNames array. |
||
287 | */ |
||
288 | public static Type custom(Class userTypeClass, String[] parameterNames, String[] parameterValues) |
||
289 | throws HibernateException { |
||
290 | Properties parameters = new Properties(); |
||
291 | for ( int i = 0; i < parameterNames.length; i++ ) { |
||
292 | parameters.setProperty( parameterNames[i], parameterValues[i] ); |
||
293 | } |
||
294 | return custom( userTypeClass, parameters ); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * A Hibernate parameterizable custom type. |
||
299 | * |
||
300 | * @param userTypeClass a class that implements <tt>UserType and ParameterizableType</tt> |
||
301 | * @param parameters the parameters as a collection of name/value pairs |
||
302 | */ |
||
303 | public static Type custom(Class userTypeClass, Properties parameters) |
||
304 | throws HibernateException { |
||
305 | if ( CompositeUserType.class.isAssignableFrom( userTypeClass ) ) { |
||
306 | CompositeCustomType type = new CompositeCustomType( userTypeClass, parameters ); |
||
307 | return type; |
||
308 | } |
||
309 | else { |
||
310 | CustomType type = new CustomType( userTypeClass, parameters ); |
||
311 | return type; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Force initialization of a proxy or persistent collection. |
||
317 | * <p/> |
||
318 | * Note: This only ensures intialization of a proxy object or collection; |
||
319 | * it is not guaranteed that the elements INSIDE the collection will be initialized/materialized. |
||
320 | * |
||
321 | * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> |
||
322 | * @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed |
||
323 | */ |
||
324 | public static void initialize(Object proxy) throws HibernateException { |
||
325 | if ( proxy == null ) { |
||
326 | return; |
||
327 | } |
||
328 | else if ( proxy instanceof HibernateProxy ) { |
||
329 | ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize(); |
||
330 | } |
||
331 | else if ( proxy instanceof PersistentCollection ) { |
||
332 | ( ( PersistentCollection ) proxy ).forceInitialization(); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Check if the proxy or persistent collection is initialized. |
||
338 | * |
||
339 | * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> |
||
340 | * @return true if the argument is already initialized, or is not a proxy or collection |
||
341 | */ |
||
342 | public static boolean isInitialized(Object proxy) { |
||
343 | if ( proxy instanceof HibernateProxy ) { |
||
344 | return !( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isUninitialized(); |
||
345 | } |
||
346 | else if ( proxy instanceof PersistentCollection ) { |
||
347 | return ( ( PersistentCollection ) proxy ).wasInitialized(); |
||
348 | } |
||
349 | else { |
||
350 | return true; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Get the true, underlying class of a proxied persistent class. This operation |
||
356 | * will initialize a proxy by side-effect. |
||
357 | * |
||
358 | * @param proxy a persistable object or proxy |
||
359 | * @return the true class of the instance |
||
360 | * @throws HibernateException |
||
361 | */ |
||
362 | public static Class getClass(Object proxy) { |
||
363 | if ( proxy instanceof HibernateProxy ) { |
||
364 | return ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer() |
||
365 | .getImplementation() |
||
366 | .getClass(); |
||
367 | } |
||
368 | else { |
||
369 | return proxy.getClass(); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Create a new <tt>Blob</tt>. The returned object will be initially immutable. |
||
375 | * |
||
376 | * @param bytes a byte array |
||
377 | * @return the Blob |
||
378 | */ |
||
379 | public static Blob createBlob(byte[] bytes) { |
||
380 | return new SerializableBlob( new BlobImpl( bytes ) ); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Create a new <tt>Blob</tt>. The returned object will be initially immutable. |
||
385 | * |
||
386 | * @param stream a binary stream |
||
387 | * @param length the number of bytes in the stream |
||
388 | * @return the Blob |
||
389 | */ |
||
390 | public static Blob createBlob(InputStream stream, int length) { |
||
391 | return new SerializableBlob( new BlobImpl( stream, length ) ); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Create a new <tt>Blob</tt>. The returned object will be initially immutable. |
||
396 | * |
||
397 | * @param stream a binary stream |
||
398 | * @return the Blob |
||
399 | * @throws IOException |
||
400 | */ |
||
401 | public static Blob createBlob(InputStream stream) throws IOException { |
||
402 | return new SerializableBlob( new BlobImpl( stream, stream.available() ) ); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Create a new <tt>Clob</tt>. The returned object will be initially immutable. |
||
407 | * |
||
408 | * @param string a <tt>String</tt> |
||
409 | */ |
||
410 | public static Clob createClob(String string) { |
||
411 | return new SerializableClob( new ClobImpl( string ) ); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Create a new <tt>Clob</tt>. The returned object will be initially immutable. |
||
416 | * |
||
417 | * @param reader a character stream |
||
418 | * @param length the number of characters in the stream |
||
419 | */ |
||
420 | public static Clob createClob(Reader reader, int length) { |
||
421 | return new SerializableClob( new ClobImpl( reader, length ) ); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Close an <tt>Iterator</tt> created by <tt>iterate()</tt> immediately, |
||
426 | * instead of waiting until the session is closed or disconnected. |
||
427 | * |
||
428 | * @param iterator an <tt>Iterator</tt> created by <tt>iterate()</tt> |
||
429 | * @throws HibernateException |
||
430 | * @see org.hibernate.Query#iterate |
||
431 | * @see Query#iterate() |
||
432 | */ |
||
433 | public static void close(Iterator iterator) throws HibernateException { |
||
434 | if ( iterator instanceof HibernateIterator ) { |
||
435 | ( ( HibernateIterator ) iterator ).close(); |
||
436 | } |
||
437 | else { |
||
438 | throw new IllegalArgumentException( "not a Hibernate iterator" ); |
||
439 | } |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Check if the property is initialized. If the named property does not exist |
||
444 | * or is not persistent, this method always returns <tt>true</tt>. |
||
445 | * |
||
446 | * @param proxy The potential proxy |
||
447 | * @param propertyName the name of a persistent attribute of the object |
||
448 | * @return true if the named property of the object is not listed as uninitialized |
||
449 | * @return false if the object is an uninitialized proxy, or the named property is uninitialized |
||
450 | */ |
||
451 | public static boolean isPropertyInitialized(Object proxy, String propertyName) { |
||
452 | |||
453 | Object entity; |
||
454 | if ( proxy instanceof HibernateProxy ) { |
||
455 | LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer(); |
||
456 | if ( li.isUninitialized() ) { |
||
457 | return false; |
||
458 | } |
||
459 | else { |
||
460 | entity = li.getImplementation(); |
||
461 | } |
||
462 | } |
||
463 | else { |
||
464 | entity = proxy; |
||
465 | } |
||
466 | |||
467 | if ( FieldInterceptionHelper.isInstrumented( entity ) ) { |
||
468 | FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor( entity ); |
||
469 | return interceptor == null || interceptor.isInitialized( propertyName ); |
||
470 | } |
||
471 | else { |
||
472 | return true; |
||
473 | } |
||
474 | |||
475 | } |
||
476 | |||
477 | } |