This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
HibernateUtils.groovy
362 lines (319 loc) · 15.6 KB
/
HibernateUtils.groovy
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.orm.hibernate.cfg
import grails.artefact.Enhanced
import grails.util.GrailsNameUtils
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.apache.commons.beanutils.PropertyUtils
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsClassUtils
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.commons.GrailsDomainClassProperty
import org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate
import org.codehaus.groovy.grails.orm.hibernate.HibernateDatastore
import org.codehaus.groovy.grails.orm.hibernate.HibernateGormEnhancer
import org.codehaus.groovy.grails.orm.hibernate.HibernateGormInstanceApi
import org.codehaus.groovy.grails.orm.hibernate.HibernateGormStaticApi
import org.codehaus.groovy.grails.orm.hibernate.HibernateGormValidationApi
import org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor
import org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin
import org.codehaus.groovy.runtime.InvokerHelper
import org.codehaus.groovy.runtime.StringGroovyMethods
import org.grails.datastore.mapping.core.Datastore
import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
import org.hibernate.FlushMode
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.proxy.HibernateProxy
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import org.springframework.core.convert.ConversionService
import org.springframework.dao.DataAccessException
import org.springframework.transaction.PlatformTransactionManager
@CompileStatic
class HibernateUtils {
static final Logger LOG = LoggerFactory.getLogger(HibernateUtils)
/**
* Overrides a getter on a property that is a Hibernate proxy in order to make sure the initialized object is returned hence avoiding Hibernate proxy hell.
*/
static void handleLazyProxy(GrailsDomainClass domainClass, GrailsDomainClassProperty property) {
String propertyName = property.name
String getterName = GrailsClassUtils.getGetterName(propertyName)
String setterName = GrailsClassUtils.getSetterName(propertyName)
GroovyObject mc = (GroovyObject)domainClass.metaClass
mc.setProperty(getterName, {->
def propertyValue = PropertyUtils.getProperty(getDelegate(), propertyName)
if (propertyValue instanceof HibernateProxy) {
propertyValue = GrailsHibernateUtil.unwrapProxy(propertyValue)
}
return propertyValue
})
mc.setProperty(setterName, { PropertyUtils.setProperty(getDelegate(), propertyName, it) })
for (GrailsDomainClass sub in domainClass.subClasses) {
handleLazyProxy(sub, sub.getPropertyByName(property.name))
}
}
static void enhanceSessionFactories(ApplicationContext ctx, GrailsApplication grailsApplication, Object source = null) {
Map<SessionFactory, HibernateDatastore> datastores = [:]
for (entry in ctx.getBeansOfType(SessionFactory).entrySet()) {
SessionFactory sessionFactory = entry.value
String beanName = entry.key
String suffix = beanName - 'sessionFactory'
enhanceSessionFactory sessionFactory, grailsApplication, ctx, suffix, datastores, source
}
ctx.getBean("eventTriggeringInterceptor", ClosureEventTriggeringInterceptor).datastores = datastores
}
static enhanceSessionFactory(SessionFactory sessionFactory, GrailsApplication application, ApplicationContext ctx) {
enhanceSessionFactory(sessionFactory, application, ctx, '', [:])
}
static enhanceSessionFactory(SessionFactory sessionFactory, GrailsApplication application,
ApplicationContext ctx, String suffix, Map<SessionFactory, HibernateDatastore> datastores, Object source = null) {
MappingContext mappingContext = ctx.getBean("grailsDomainClassMappingContext", MappingContext)
PlatformTransactionManager transactionManager = ctx.getBean("transactionManager$suffix", PlatformTransactionManager)
HibernateDatastore datastore = (HibernateDatastore)ctx.getBean("hibernateDatastore$suffix", Datastore)
datastores[sessionFactory] = datastore
String datasourceName = suffix ? suffix[1..-1] : GrailsDomainClassProperty.DEFAULT_DATA_SOURCE
HibernateGormEnhancer enhancer = new HibernateGormEnhancer(datastore, transactionManager, application)
def enhanceEntity = { PersistentEntity entity ->
GrailsDomainClass dc = (GrailsDomainClass)application.getArtefact(DomainClassArtefactHandler.TYPE, entity.javaClass.name)
if (!GrailsHibernateUtil.isMappedWithHibernate(dc) || !GrailsHibernateUtil.usesDatasource(dc, datasourceName)) {
return
}
if (!datasourceName.equals(GrailsDomainClassProperty.DEFAULT_DATA_SOURCE)) {
LOG.debug "Registering namespace methods for $dc.clazz.name in DataSource '$datasourceName'"
registerNamespaceMethods dc, datastore, datasourceName, transactionManager, application
}
if (datasourceName.equals(GrailsDomainClassProperty.DEFAULT_DATA_SOURCE) || datasourceName.equals(GrailsHibernateUtil.getDefaultDataSource(dc))) {
LOG.debug "Enhancing GORM entity ${entity.name}"
if (entity.javaClass.getAnnotation(Enhanced) == null) {
enhancer.enhance entity
}
else {
enhancer.enhance entity, true
}
DomainClassGrailsPlugin.addRelationshipManagementMethods(dc, ctx)
}
}
// If we are reloading via an onChange event, the source indicates the specific
// entity that needs to be reloaded. Otherwise, just reload all of them.
if (source) {
PersistentEntity entity = getPersistentEntity(mappingContext, InvokerHelper.getPropertySafe(source, 'name')?.toString())
if (entity) {
enhanceEntity(entity)
}
}
else {
for (PersistentEntity entity in mappingContext.getPersistentEntities()) {
enhanceEntity(entity)
}
}
}
// workaround CS bug
@CompileStatic(TypeCheckingMode.SKIP)
private static PersistentEntity getPersistentEntity(mappingContext, String name) {
mappingContext.getPersistentEntity(name)
}
static Map filterQueryArgumentMap(Map query) {
def queryArgs = [:]
for (entry in query.entrySet()) {
if (entry.value instanceof CharSequence) {
queryArgs[entry.key] = entry.value.toString()
}
else {
queryArgs[entry.key] = entry.value
}
}
return queryArgs
}
private static List<String> removeNullNames(Map query) {
List<String> nullNames = []
Set<String> allNames = new HashSet(query.keySet())
for (String name in allNames) {
if (query[name] == null) {
query.remove name
nullNames << name
}
}
nullNames
}
// http://jira.codehaus.org/browse/GROOVY-6138 prevents using CompileStatic for this method
@CompileStatic(TypeCheckingMode.SKIP)
static void enhanceProxyClass(Class proxyClass) {
MetaMethod grailsEnhancedMetaMethod = proxyClass.metaClass.getStaticMetaMethod("grailsEnhanced", (Class[])null)
if (grailsEnhancedMetaMethod != null && grailsEnhancedMetaMethod.invoke(proxyClass, null) == proxyClass) {
return
}
GroovyObject mc = (GroovyObject)InvokerHelper.getMetaClass(proxyClass)
MetaClass superMc = InvokerHelper.getMetaClass(proxyClass.getSuperclass())
// hasProperty
registerMetaMethod(mc, 'hasProperty', { String name ->
Object obj = getDelegate()
boolean result = superMc.hasProperty(obj, name)
if (!result) {
Object unwrapped = GrailsHibernateUtil.unwrapProxy((HibernateProxy)obj)
result = unwrapped.getMetaClass().hasProperty(obj, name)
}
return result
})
// respondsTo
registerMetaMethod(mc, 'respondsTo', { String name ->
Object obj = getDelegate()
def result = superMc.respondsTo(obj, name)
if (!result) {
Object unwrapped = GrailsHibernateUtil.unwrapProxy((HibernateProxy)obj)
result = unwrapped.getMetaClass().respondsTo(obj, name)
}
result
})
registerMetaMethod(mc, 'respondsTo', { String name, Object[] args ->
Object obj = getDelegate()
def result = superMc.respondsTo(obj, name, args)
if (!result) {
Object unwrapped = GrailsHibernateUtil.unwrapProxy((HibernateProxy)obj)
result = unwrapped.getMetaClass().respondsTo(obj, name, args)
}
result
})
// setter
registerMetaMethod(mc, 'propertyMissing', { String name, Object val ->
Object obj = getDelegate()
try {
superMc.setProperty(proxyClass, obj, name, val, true, true)
} catch (MissingPropertyException e) {
Object unwrapped = GrailsHibernateUtil.unwrapProxy((HibernateProxy)obj)
unwrapped.getMetaClass().setProperty(unwrapped, name, val)
}
})
// getter
registerMetaMethod(mc, 'propertyMissing', { String name ->
Object obj = getDelegate()
try {
return superMc.getProperty(proxyClass, obj, name, true, true)
} catch (MissingPropertyException e) {
Object unwrapped = GrailsHibernateUtil.unwrapProxy((HibernateProxy)obj)
unwrapped.getMetaClass().getProperty(unwrapped, name)
}
})
registerMetaMethod(mc, 'methodMissing', { String name, Object args ->
Object obj = getDelegate()
Object[] argsArray = (Object[])args
try {
superMc.invokeMethod(proxyClass, obj, name, argsArray, true, true)
} catch (MissingMethodException e) {
Object unwrapped = GrailsHibernateUtil.unwrapProxy((HibernateProxy)obj)
unwrapped.getMetaClass().invokeMethod(unwrapped, name, argsArray)
}
})
((GroovyObject)mc.getProperty('static')).setProperty("grailsEnhanced", { -> proxyClass })
}
private static final registerMetaMethod(MetaClass mc, String name, Closure c) {
((GroovyObject)mc).setProperty(name, c)
}
static void enhanceProxy(HibernateProxy proxy) {
// no need to do anything here
}
private static void registerNamespaceMethods(GrailsDomainClass dc, HibernateDatastore datastore,
String datasourceName, PlatformTransactionManager transactionManager,
GrailsApplication application) {
String getter = GrailsNameUtils.getGetterName(datasourceName)
if (dc.metaClass.methods.any { MetaMethod it -> it.name == getter && it.parameterTypes.size() == 0 }) {
LOG.warn "The $dc.clazz.name domain class has a method '$getter' - unable to add namespaced methods for datasource '$datasourceName'"
return
}
def classLoader = application.classLoader
def finders = new HibernateGormEnhancer(datastore, transactionManager, application).getFinders()
def staticApi = new HibernateGormStaticApi(dc.clazz, datastore, finders, classLoader, transactionManager)
((GroovyObject)((GroovyObject)dc.metaClass).getProperty('static')).setProperty(getter, { -> staticApi })
def validateApi = new HibernateGormValidationApi(dc.clazz, datastore, classLoader)
def instanceApi = new HibernateGormInstanceApi(dc.clazz, datastore, classLoader)
((GroovyObject)dc.metaClass).setProperty(getter, { -> new InstanceProxy(getDelegate(), instanceApi, validateApi) })
}
/**
* Session should no longer be flushed after a data access exception occurs (such a constriant violation)
*/
static void handleDataAccessException(GrailsHibernateTemplate template, DataAccessException e) {
try {
template.execute new GrailsHibernateTemplate.HibernateCallback() {
def doInHibernate(Session session) {
session.setFlushMode(FlushMode.MANUAL)
}
}
}
finally {
throw e
}
}
static shouldFlush(GrailsApplication application, Map map = [:]) {
def shouldFlush
if (map?.containsKey('flush')) {
shouldFlush = Boolean.TRUE == map.flush
} else {
def config = application.flatConfig
shouldFlush = Boolean.TRUE == config.get('grails.gorm.autoFlush')
}
return shouldFlush
}
/**
* Converts an id value to the appropriate type for a domain class.
*
* @param grailsDomainClass a GrailsDomainClass
* @param idValue an value to be converted
* @return the idValue parameter converted to the type that grailsDomainClass expects
* its identifiers to be
*/
static Object convertValueToIdentifierType(GrailsDomainClass grailsDomainClass, Object idValue, ConversionService conversionService) {
convertValueToType(idValue, grailsDomainClass.identifier.type, conversionService)
}
static Object convertValueToType(Object passedValue, Class targetType, ConversionService conversionService) {
// workaround for GROOVY-6127, do not assign directly in parameters before it's fixed
Object value = passedValue
if(targetType != null && value != null && !(value in targetType)) {
if (value instanceof CharSequence) {
value = value.toString()
if(value in targetType) {
return value
}
}
try {
if (value instanceof Number && (targetType==Long || targetType==Integer)) {
if(targetType == Long) {
value = ((Number)value).toLong()
} else {
value = ((Number)value).toInteger()
}
} else if (value instanceof String && targetType in Number) {
String strValue = value.trim()
if(targetType == Long) {
value = Long.parseLong(strValue)
} else if (targetType == Integer) {
value = Integer.parseInt(strValue)
} else {
value = StringGroovyMethods.asType(strValue, targetType)
}
} else {
value = conversionService.convert(value, targetType)
}
} catch (e) {
// ignore
}
}
return value
}
}