-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Hibernate.java
198 lines (183 loc) · 6.3 KB
/
Hibernate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate;
import java.util.Iterator;
import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper;
import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.HibernateIterator;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
/**
* <ul>
* <li>Provides access to the full range of Hibernate built-in types. <tt>Type</tt>
* instances may be used to bind values to query parameters.
* <li>A factory for new <tt>Blob</tt>s and <tt>Clob</tt>s.
* <li>Defines static methods for manipulation of proxies.
* </ul>
*
* @author Gavin King
* @see java.sql.Clob
* @see java.sql.Blob
* @see org.hibernate.type.Type
*/
public final class Hibernate {
/**
* Cannot be instantiated.
*/
private Hibernate() {
throw new UnsupportedOperationException();
}
/**
* Force initialization of a proxy or persistent collection.
* <p/>
* Note: This only ensures intialization of a proxy object or collection;
* it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.
*
* @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
* @throws HibernateException if we can't initialize the proxy at this time, eg. the <tt>Session</tt> was closed
*/
public static void initialize(Object proxy) throws HibernateException {
if ( proxy == null ) {
return;
}
if ( proxy instanceof HibernateProxy ) {
( (HibernateProxy) proxy ).getHibernateLazyInitializer().initialize();
}
else if ( proxy instanceof PersistentCollection ) {
( (PersistentCollection) proxy ).forceInitialization();
}
}
/**
* Check if the proxy or persistent collection is initialized.
*
* @param proxy a persistable object, proxy, persistent collection or <tt>null</tt>
* @return true if the argument is already initialized, or is not a proxy or collection
*/
@SuppressWarnings("SimplifiableIfStatement")
public static boolean isInitialized(Object proxy) {
if ( proxy instanceof HibernateProxy ) {
return !( (HibernateProxy) proxy ).getHibernateLazyInitializer().isUninitialized();
}
else if ( proxy instanceof PersistentCollection ) {
return ( (PersistentCollection) proxy ).wasInitialized();
}
else {
return true;
}
}
/**
* Get the true, underlying class of a proxied persistent class. This operation
* will initialize a proxy by side-effect.
*
* @param proxy a persistable object or proxy
* @return the true class of the instance
* @throws HibernateException
*/
public static Class getClass(Object proxy) {
if ( proxy instanceof HibernateProxy ) {
return ( (HibernateProxy) proxy ).getHibernateLazyInitializer()
.getImplementation()
.getClass();
}
else {
return proxy.getClass();
}
}
/**
* Obtain a lob creator for the given session.
*
* @param session The session for which to obtain a lob creator
*
* @return The log creator reference
*/
public static LobCreator getLobCreator(Session session) {
return getLobCreator( (SessionImplementor) session );
}
/**
* Obtain a lob creator for the given session.
*
* @param session The session for which to obtain a lob creator
*
* @return The log creator reference
*/
public static LobCreator getLobCreator(SessionImplementor session) {
return session.getFactory()
.getServiceRegistry()
.getService( JdbcServices.class )
.getLobCreator( session );
}
/**
* Close an {@link Iterator} instances obtained from {@link org.hibernate.Query#iterate()} immediately
* instead of waiting until the session is closed or disconnected.
*
* @param iterator an Iterator created by iterate()
*
* @throws HibernateException Indicates a problem closing the Hibernate iterator.
* @throws IllegalArgumentException If the Iterator is not a "Hibernate Iterator".
*
* @see Query#iterate()
*/
public static void close(Iterator iterator) throws HibernateException {
if ( iterator instanceof HibernateIterator ) {
( (HibernateIterator) iterator ).close();
}
else {
throw new IllegalArgumentException( "not a Hibernate iterator" );
}
}
/**
* Check if the property is initialized. If the named property does not exist
* or is not persistent, this method always returns <tt>true</tt>.
*
* @param proxy The potential proxy
* @param propertyName the name of a persistent attribute of the object
* @return true if the named property of the object is not listed as uninitialized; false otherwise
*/
public static boolean isPropertyInitialized(Object proxy, String propertyName) {
final Object entity;
if ( proxy instanceof HibernateProxy ) {
final LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
if ( li.isUninitialized() ) {
return false;
}
else {
entity = li.getImplementation();
}
}
else {
entity = proxy;
}
if ( FieldInterceptionHelper.isInstrumented( entity ) ) {
final FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor( entity );
return interceptor == null || interceptor.isInitialized( propertyName );
}
else {
return true;
}
}
}