-
Notifications
You must be signed in to change notification settings - Fork 68
/
AbstractHibernateGormInstanceApi.groovy
494 lines (436 loc) · 17.2 KB
/
AbstractHibernateGormInstanceApi.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* 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.grails.orm.hibernate
import grails.gorm.validation.CascadingValidator
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import org.grails.datastore.gorm.GormValidateable
import org.grails.datastore.mapping.core.Datastore
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable
import org.grails.datastore.mapping.model.config.GormProperties
import org.grails.datastore.mapping.model.types.Embedded
import org.grails.datastore.mapping.model.types.EmbeddedCollection
import org.grails.datastore.mapping.model.types.ToMany
import org.grails.datastore.mapping.proxy.ProxyHandler
import org.grails.datastore.mapping.reflect.ClassUtils
import org.grails.datastore.mapping.reflect.EntityReflector
import org.grails.orm.hibernate.support.HibernateRuntimeUtils
import org.grails.datastore.gorm.GormInstanceApi
import org.grails.datastore.mapping.engine.event.ValidationEvent
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.datastore.mapping.model.PersistentProperty
import org.grails.datastore.mapping.model.types.Association
import org.grails.datastore.mapping.model.types.ToOne
import org.hibernate.*
import org.springframework.beans.BeanWrapperImpl
import org.springframework.beans.InvalidPropertyException
import org.springframework.dao.DataAccessException
import org.springframework.validation.Errors
import org.springframework.validation.Validator
/**
* Abstract extension of the {@link GormInstanceApi} class that provides common logic shared by Hibernate 3 and Hibernate 4
*
* @author Graeme Rocher
* @param <D>
*/
@CompileStatic
abstract class AbstractHibernateGormInstanceApi<D> extends GormInstanceApi<D> {
private static final String ARGUMENT_VALIDATE = "validate"
private static final String ARGUMENT_DEEP_VALIDATE = "deepValidate"
private static final String ARGUMENT_FLUSH = "flush"
private static final String ARGUMENT_INSERT = "insert"
private static final String ARGUMENT_MERGE = "merge"
private static final String ARGUMENT_FAIL_ON_ERROR = "failOnError"
private static final Class DEFERRED_BINDING
static {
try {
DEFERRED_BINDING = Class.forName('grails.validation.DeferredBindingActions')
} catch (Throwable e) {
DEFERRED_BINDING = null
}
}
protected static final Object[] EMPTY_ARRAY = []
/**
* When a domain instance is saved without validation, we put it
* into this thread local variable. Any code that needs to know
* whether the domain instance should be validated can just check
* the value. Note that this only works because the session is
* flushed when a domain instance is saved without validation.
*/
static final ThreadLocal<Boolean> insertActiveThreadLocal = new ThreadLocal<Boolean>()
protected SessionFactory sessionFactory
protected ClassLoader classLoader
protected IHibernateTemplate hibernateTemplate
protected ProxyHandler proxyHandler
boolean autoFlush
protected AbstractHibernateGormInstanceApi(Class<D> persistentClass, AbstractHibernateDatastore datastore, ClassLoader classLoader, IHibernateTemplate hibernateTemplate) {
super(persistentClass, datastore)
this.classLoader = classLoader
sessionFactory = datastore.getSessionFactory()
this.hibernateTemplate = hibernateTemplate
this.proxyHandler = datastore.mappingContext.getProxyHandler()
this.autoFlush = datastore.autoFlush
this.failOnError = datastore.failOnError
this.markDirty = datastore.markDirty
}
@Override
D save(D target, Map arguments) {
PersistentEntity domainClass = persistentEntity
runDeferredBinding()
boolean shouldFlush = shouldFlush(arguments)
boolean shouldValidate = shouldValidate(arguments, persistentEntity)
HibernateRuntimeUtils.autoAssociateBidirectionalOneToOnes(domainClass, target)
boolean deepValidate = true
if (arguments?.containsKey(ARGUMENT_DEEP_VALIDATE)) {
deepValidate = ClassUtils.getBooleanFromMap(ARGUMENT_DEEP_VALIDATE, arguments)
}
if (shouldValidate) {
Validator validator = datastore.mappingContext.getEntityValidator(domainClass)
Errors errors = HibernateRuntimeUtils.setupErrorsProperty(target)
if (validator) {
datastore.applicationEventPublisher?.publishEvent new ValidationEvent(datastore, target)
if (validator instanceof CascadingValidator) {
((CascadingValidator)validator).validate target, errors, deepValidate
} else if (validator instanceof org.grails.datastore.gorm.validation.CascadingValidator) {
((org.grails.datastore.gorm.validation.CascadingValidator) validator).validate target, errors, deepValidate
} else {
validator.validate target, errors
}
if (errors.hasErrors()) {
handleValidationError(domainClass,target,errors)
if (shouldFail(arguments)) {
throw validationException.newInstance("Validation Error(s) occurred during save()", errors)
}
return null
}
setObjectToReadWrite(target)
}
}
// this piece of code will retrieve a persistent instant
// of a domain class property is only the id is set thus
// relieving this burden off the developer
autoRetrieveAssocations datastore, domainClass, target
// If validation is disabled, skip it or if a flush:true is passed then disable it too to avoid duplicate validation
GormValidateable validateable = (GormValidateable) target
boolean shouldSkipValidation = !shouldValidate || shouldFlush
validateable.skipValidation(shouldSkipValidation)
try {
if (shouldInsert(arguments)) {
return performInsert(target, shouldFlush)
}
else if(shouldMerge(arguments)) {
return performMerge(target, shouldFlush)
}
else {
if (target instanceof DirtyCheckable && markDirty) {
target.markDirty()
}
return performSave(target, shouldFlush)
}
} finally {
validateable.skipValidation(!shouldFlush)
}
}
@CompileDynamic
private void runDeferredBinding() {
DEFERRED_BINDING?.runActions()
}
@Override
D merge(D instance, Map params) {
Map args = new HashMap(params)
args[ARGUMENT_MERGE] = true
return save(instance, args)
}
@Override
D insert(D instance, Map params) {
Map args = new HashMap(params)
args[ARGUMENT_INSERT] = true
return save(instance, args)
}
@Override
void discard(D instance) {
hibernateTemplate.evict instance
}
@Override
void delete(D instance, Map params = Collections.emptyMap()) {
boolean flush = shouldFlush(params)
try {
hibernateTemplate.execute { Session session ->
session.delete instance
if(flush) {
session.flush()
}
}
}
catch (DataAccessException e) {
try {
hibernateTemplate.execute { Session session ->
session.flushMode = FlushMode.MANUAL
}
}
finally {
throw e
}
}
}
@Override
boolean isAttached(D instance) {
hibernateTemplate.contains instance
}
@Override
boolean instanceOf(D instance, Class cls) {
return proxyHandler.unwrap(instance) in cls
}
@Override
D lock(D instance) {
hibernateTemplate.lock(instance, LockMode.PESSIMISTIC_WRITE)
instance
}
@Override
D attach(D instance) {
hibernateTemplate.lock(instance, LockMode.NONE)
return instance
}
@Override
D refresh(D instance) {
hibernateTemplate.refresh(instance)
return instance
}
protected D performSave(final D target, final boolean flush) {
hibernateTemplate.execute { Session session ->
session.saveOrUpdate target
if (flush) {
flushSession session
}
return target
}
}
protected D performMerge(final D target, final boolean flush) {
hibernateTemplate.execute { Session session ->
Object merged = session.merge(target)
session.lock(merged, LockMode.NONE)
if (flush) {
flushSession session
}
return (D)merged
}
}
protected D performInsert(final D target, final boolean shouldFlush) {
hibernateTemplate.execute { Session session ->
try {
markInsertActive()
session.save target
if (shouldFlush) {
flushSession session
}
return target
} finally {
resetInsertActive()
}
}
}
protected void flushSession(Session session) throws HibernateException {
try {
session.flush()
} catch (HibernateException e) {
// session should not be flushed again after a data acccess exception!
session.setFlushMode FlushMode.MANUAL
throw e
}
}
/**
* Performs automatic association retrieval
* @param entity The domain class to retrieve associations for
* @param target The target object
*/
@SuppressWarnings("unchecked")
private void autoRetrieveAssocations(Datastore datastore, PersistentEntity entity, Object target) {
EntityReflector reflector = datastore.mappingContext.getEntityReflector(entity)
IHibernateTemplate t = this.hibernateTemplate
for (PersistentProperty prop in entity.associations) {
if(prop instanceof ToOne && !(prop instanceof Embedded)) {
ToOne toOne = (ToOne)prop
def propertyName = prop.name
def propValue = reflector.getProperty(target, propertyName)
if (propValue == null || t.contains(propValue)) {
continue
}
PersistentEntity otherSide = toOne.associatedEntity
if (otherSide == null) {
continue
}
def identity = otherSide.identity
if(identity == null) {
continue
}
def otherSideReflector = datastore.mappingContext.getEntityReflector(otherSide)
try {
def id = (Serializable)otherSideReflector.getProperty(propValue, identity.name);
if (id) {
final Object associatedInstance = t.get(prop.type, id)
if (associatedInstance) {
reflector.setProperty(target, propertyName, associatedInstance)
}
}
}
catch (InvalidPropertyException ipe) {
// property is not accessable
}
}
}
}
/**
* Checks whether validation should be performed
* @return true if the domain class should be validated
* @param arguments The arguments to the validate method
* @param domainClass The domain class
*/
private boolean shouldValidate(Map arguments, PersistentEntity entity) {
if (!entity) {
return false
}
if (arguments?.containsKey(ARGUMENT_VALIDATE)) {
return ClassUtils.getBooleanFromMap(ARGUMENT_VALIDATE, arguments)
}
return true
}
private boolean shouldInsert(Map arguments) {
ClassUtils.getBooleanFromMap(ARGUMENT_INSERT, arguments)
}
private boolean shouldMerge(Map arguments) {
ClassUtils.getBooleanFromMap(ARGUMENT_MERGE, arguments)
}
protected boolean shouldFlush(Map map) {
if (map?.containsKey(ARGUMENT_FLUSH)) {
return ClassUtils.getBooleanFromMap(ARGUMENT_FLUSH, map)
}
return autoFlush
}
protected boolean shouldFail(Map map) {
if (map?.containsKey(ARGUMENT_FAIL_ON_ERROR)) {
return ClassUtils.getBooleanFromMap(ARGUMENT_FAIL_ON_ERROR, map)
}
return failOnError
}
/**
* Sets the flush mode to manual. which ensures that the database changes are not persisted to the database
* if a validation error occurs. If save() is called again and validation passes the code will check if there
* is a manual flush mode and flush manually if necessary
*
* @param domainClass The domain class
* @param target The target object that failed validation
* @param errors The Errors instance @return This method will return null signaling a validation failure
*/
protected Object handleValidationError(PersistentEntity entity, final Object target, Errors errors) {
// if a validation error occurs set the object to read-only to prevent a flush
setObjectToReadOnly target
if (entity) {
for (Association association in entity.associations) {
if (association instanceof ToOne && !association instanceof Embedded) {
if(proxyHandler.isInitialized(target, association.name)) {
def bean = new BeanWrapperImpl(target)
def propertyValue = bean.getPropertyValue(association.name)
if (propertyValue != null) {
setObjectToReadOnly propertyValue
}
}
}
}
}
setErrorsOnInstance target, errors
return null
}
/**
* Sets the target object to read-only using the given SessionFactory instance. This
* avoids Hibernate performing any dirty checking on the object
*
*
* @param target The target object
* @param sessionFactory The SessionFactory instance
*/
void setObjectToReadOnly(Object target) {
hibernateTemplate.execute { Session session ->
if (session.contains(target) && proxyHandler.isInitialized(target)) {
target = proxyHandler.unwrap(target)
session.setReadOnly target, true
session.flushMode = FlushMode.MANUAL
}
}
}
/**
* Sets the target object to read-write, allowing Hibernate to dirty check it and auto-flush changes.
*
* @see #setObjectToReadOnly(Object)
*
* @param target The target object
* @param sessionFactory The SessionFactory instance
*/
abstract void setObjectToReadWrite(Object target)
/**
* Associates the Errors object on the instance
*
* @param target The target instance
* @param errors The Errors object
*/
@CompileDynamic
protected void setErrorsOnInstance(Object target, Errors errors) {
if(target instanceof GormValidateable) {
((GormValidateable)target).setErrors(errors)
}
else {
target."$GormProperties.ERRORS" = errors
}
}
/**
* Prevents hitting the database for an extra check if the row exists in the database.
*
* ThreadLocal is used to pass the "insert:true" information to Hibernate.
*
* @see org.hibernate.event.def.AbstractSaveEventListener#getAssumedUnsaved()
*/
static Boolean getAssumedUnsaved() {
return insertActiveThreadLocal.get();
}
/**
* Called by org.grails.orm.hibernate.metaclass.SavePersistentMethod's performInsert
* to set a ThreadLocal variable that determines the value for getAssumedUnsaved().
*/
static void markInsertActive() {
insertActiveThreadLocal.set(Boolean.TRUE);
}
/**
* Clears the ThreadLocal variable set by markInsertActive().
*/
static void resetInsertActive() {
insertActiveThreadLocal.remove();
}
/**
* Increments the entities version number in order to force an update
* @param target The target entity
*/
@CompileDynamic
protected void incrementVersion(Object target) {
if (target.hasProperty(GormProperties.VERSION)) {
Object version = target."${GormProperties.VERSION}"
if (version instanceof Long) {
target."${GormProperties.VERSION}" = ++((Long)version)
}
}
}
SessionFactory getSessionFactory() {
return this.sessionFactory
}
}