/
GormEntity.groovy
1332 lines (1197 loc) · 39.9 KB
/
GormEntity.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 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.datastore.gorm
import grails.gorm.DetachedCriteria
import groovy.transform.CompileStatic
import org.grails.datastore.gorm.finders.FinderMethod
import org.grails.datastore.gorm.query.GormQueryOperations
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable
import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.datastore.mapping.model.types.Association
import org.grails.datastore.mapping.model.types.Basic
import org.grails.datastore.mapping.model.types.ManyToMany
import org.grails.datastore.mapping.model.types.OneToMany
import org.grails.datastore.mapping.model.types.ToOne
import org.grails.datastore.mapping.query.api.BuildableCriteria
import org.grails.datastore.mapping.query.api.Criteria
import org.grails.datastore.mapping.reflect.ClassPropertyFetcher
import org.grails.datastore.mapping.reflect.EntityReflector
import org.springframework.transaction.TransactionDefinition
import javax.persistence.Transient
/**
*
* A trait that turns any class into a GORM entity
*
* @author Jeff Brown
* @authro Graeme Rocher
* @since 4.0
*/
@CompileStatic
trait GormEntity<D> implements GormValidateable, DirtyCheckable, GormEntityApi<D> {
/**
* Allow access to datasource by name
*
* @param instance The instance
* @param name The property name
* @return The property value
*/
def propertyMissing(String name) {
GormEnhancer.findInstanceApi(getClass()).propertyMissing(this, name)
}
/**
* Proxy aware instanceOf implementation.
*/
boolean instanceOf(Class cls) {
currentGormInstanceApi().instanceOf this, cls
}
/**
* Upgrades an existing persistence instance to a write lock
* @return The instance
*/
D lock() {
currentGormInstanceApi().lock this
}
/**
* Locks the instance for updates for the scope of the passed closure
*
* @param callable The closure
* @return The result of the closure
*/
def mutex(Closure callable) {
currentGormInstanceApi().mutex this, callable
}
/**
* Refreshes the state of the current instance
* @return The instance
*/
D refresh() {
currentGormInstanceApi().refresh this
}
/**
* Saves an object the datastore
* @return Returns the instance
*/
D save() {
currentGormInstanceApi().save this
}
/**
* Forces an insert of an object to the datastore
* @return Returns the instance
*/
D insert() {
currentGormInstanceApi().insert this
}
/**
* Forces an insert of an object to the datastore
* @return Returns the instance
*/
D insert(Map params) {
currentGormInstanceApi().insert this, params
}
/**
* Saves an object the datastore
* @return Returns the instance
*/
D merge() {
currentGormInstanceApi().merge this
}
/**
* Saves an object the datastore
* @return Returns the instance
*/
D merge(Map params) {
currentGormInstanceApi().merge this, params
}
/**
* Save method that takes a boolean which indicates whether to perform validation or not
*
* @param validate Whether to perform validation
*
* @return The instance or null if validation fails
*/
D save(boolean validate) {
currentGormInstanceApi().save this, validate
}
/**
* Saves an object with the given parameters
* @param instance The instance
* @param params The parameters
* @return The instance
*/
D save(Map params) {
currentGormInstanceApi().save this, params
}
/**
* Returns the objects identifier
*/
Serializable ident() {
currentGormInstanceApi().ident this
}
/**
* Attaches an instance to an existing session. Requries a session-based model
* @return
*/
D attach() {
currentGormInstanceApi().attach this
}
/**
* No concept of session-based model so defaults to true
*/
@Transient
boolean isAttached() {
currentGormInstanceApi().isAttached this
}
/**
* Discards any pending changes. Requires a session-based model.
*/
void discard() {
currentGormInstanceApi().discard this
}
/**
* Deletes an instance from the datastore
*/
void delete() {
currentGormInstanceApi().delete this
}
/**
* Deletes an instance from the datastore
*/
void delete(Map params) {
currentGormInstanceApi().delete this, params
}
/**
* Checks whether a field is dirty
*
* @param instance The instance
* @param fieldName The name of the field
*
* @return true if the field is dirty
*/
boolean isDirty(String fieldName) {
currentGormInstanceApi().isDirty this, fieldName
}
/**
* Checks whether an entity is dirty
*
* @param instance The instance
* @return true if it is dirty
*/
@Transient
boolean isDirty() {
currentGormInstanceApi().isDirty this
}
/**
* Obtains a list of property names that are dirty
*
* @param instance The instance
* @return A list of property names that are dirty
*/
@Transient
List getDirtyPropertyNames() {
currentGormInstanceApi().getDirtyPropertyNames this
}
/**
* Gets the original persisted value of a field.
*
* @param fieldName The field name
* @return The original persisted value
*/
Object getPersistentValue(String fieldName) {
currentGormInstanceApi().getPersistentValue this, fieldName
}
/**
* Obtains the id of an association without initialising the association
*
* @param associationName The association name
* @return The id of the association or null if it doesn't have one
*/
Serializable getAssociationId(String associationName) {
PersistentEntity entity = getGormPersistentEntity()
MappingContext mappingContext = entity.mappingContext
EntityReflector entityReflector = mappingContext.getEntityReflector(entity)
def association = entity.getPropertyByName(associationName)
if(association instanceof ToOne) {
def proxyHandler = mappingContext.getProxyHandler()
def value = entityReflector.getProperty(this, associationName)
if(proxyHandler.isProxy(value)) {
return proxyHandler.getIdentifier(value)
}
else {
PersistentEntity associatedEntity = ((ToOne)association).getAssociatedEntity()
if(associatedEntity != null) {
return associatedEntity.getReflector().getIdentifier(value)
}
}
}
return null
}
/**
* Removes the given value to given association ensuring both sides are correctly disassociated
*
* @param associationName The association name
* @param arg The value
* @return This domain instance
*/
D removeFrom(String associationName, Object arg) {
final PersistentEntity entity = getGormPersistentEntity()
def prop = entity.getPropertyByName(associationName)
final MappingContext mappingContext = entity.mappingContext
final EntityReflector entityReflector = mappingContext.getEntityReflector(entity)
if(prop instanceof Association) {
Association association = (Association)prop
final javaClass = association.associatedEntity?.javaClass
final boolean isBasic = association instanceof Basic
if(isBasic) {
javaClass = ((Basic)association).componentType
}
if (javaClass.isInstance(arg)) {
final propertyName = prop.name
Collection currentValue = (Collection)entityReflector.getProperty(this, propertyName)
currentValue?.remove(arg)
markDirty(propertyName)
if (association.bidirectional) {
def otherSide = association.inverseSide
def associationReflector = mappingContext.getEntityReflector(association.associatedEntity)
if (otherSide instanceof ManyToMany) {
Collection otherSideValue = (Collection) associationReflector.getProperty(arg, otherSide.name)
otherSideValue?.remove(this)
}
else {
associationReflector.setProperty(arg, otherSide.name, null)
}
}
}
else {
throw new IllegalArgumentException("")
}
}
return (D)this
}
/**
* Adds the given value to given association ensuring both sides are correctly associated
*
* @param associationName The association name
* @param arg The value
* @return This domain instance
*/
D addTo(String associationName, Object arg) {
final PersistentEntity entity = getGormPersistentEntity()
final def prop = entity.getPropertyByName(associationName)
final D targetObject = (D)this
final MappingContext mappingContext = entity.mappingContext
final EntityReflector reflector = mappingContext.getEntityReflector(entity)
if(reflector != null && (prop instanceof Association)) {
final Association association = (Association)prop
final propertyName = association.name
def obj
def currentValue = reflector.getProperty(targetObject, propertyName)
if (currentValue == null) {
currentValue = [].asType(prop.type)
reflector.setProperty(targetObject, propertyName, currentValue)
}
final javaClass = association.associatedEntity?.javaClass
final boolean isBasic = association instanceof Basic
if(isBasic) {
javaClass = ((Basic)association).componentType
}
if (arg instanceof Map) {
obj = javaClass.newInstance(arg)
}
else if (javaClass.isInstance(arg)) {
obj = arg
}
else {
def conversionService = mappingContext.conversionService
if(conversionService.canConvert(arg.getClass(), javaClass)) {
obj = conversionService.convert(arg, javaClass)
}
else {
throw new IllegalArgumentException("Cannot add value [$arg] to collection [$propertyName] with type [$javaClass.name]")
}
}
def coll = (Collection)currentValue
coll.add(obj)
markDirty(propertyName)
if (isBasic) {
return targetObject
}
if (association.bidirectional && association.inverseSide) {
def otherSide = association.inverseSide
String name = otherSide.name
def associationReflector = mappingContext.getEntityReflector(association.associatedEntity)
if (otherSide instanceof OneToMany || otherSide instanceof ManyToMany) {
Collection otherSideValue = (Collection)associationReflector.getProperty(obj, name)
if (otherSideValue == null) {
otherSideValue = (Collection)( [].asType(otherSide.type) )
associationReflector.setProperty(obj, name, otherSideValue)
}
otherSideValue.add(targetObject)
if(obj instanceof DirtyCheckable) {
((DirtyCheckable)obj).markDirty(name)
}
}
else {
associationReflector?.setProperty(obj, name, targetObject)
if(obj instanceof DirtyCheckable) {
((DirtyCheckable)obj).markDirty(name)
}
}
}
targetObject
}
return targetObject
}
private MappingContext lookupMappingContext() {
currentGormStaticApi().datastore.mappingContext
}
/**
* @return The PersistentEntity for this class
*/
static PersistentEntity getGormPersistentEntity() {
currentGormStaticApi().persistentEntity
}
static List<FinderMethod> getGormDynamicFinders() {
currentGormStaticApi().gormDynamicFinders
}
/**
*
* @param callable Callable closure containing detached criteria definition
* @return The DetachedCriteria instance
*/
static DetachedCriteria<D> where(Closure callable) {
currentGormStaticApi().where callable
}
/**
*
* @param callable Callable closure containing detached criteria definition
* @return The DetachedCriteria instance that is lazily initialized
*/
static DetachedCriteria<D> whereLazy(Closure callable) {
currentGormStaticApi().whereLazy callable
}
/**
*
* @param callable Callable closure containing detached criteria definition
* @return The DetachedCriteria instance
*/
static DetachedCriteria<D> whereAny(Closure callable) {
currentGormStaticApi().whereAny callable
}
/**
* Uses detached criteria to build a query and then execute it returning a list
*
* @param callable The callable
* @return A List of entities
*/
static List<D> findAll(Closure callable) {
currentGormStaticApi().findAll callable
}
/**
* Uses detached criteria to build a query and then execute it returning a list
*
* @param args pagination parameters
* @param callable The callable
* @return A List of entities
*/
static List<D> findAll(Map args, Closure callable) {
currentGormStaticApi().findAll args, callable
}
/**
* Uses detached criteria to build a query and then execute it returning a list
*
* @param callable The callable
* @return A single entity
*/
static D find(Closure callable) {
currentGormStaticApi().find callable
}
/**
* Saves a list of objects in one go
* @param objectsToSave The objects to save
* @return A list of object identifiers
*/
static List<Serializable> saveAll(Object... objectsToSave) {
currentGormStaticApi().saveAll objectsToSave
}
/**
* Saves a list of objects in one go
* @param objectToSave Collection of objects to save
* @return A list of object identifiers
*/
static List<Serializable> saveAll(Iterable<?> objectsToSave) {
currentGormStaticApi().saveAll objectsToSave
}
/**
* Deletes a list of objects in one go
* @param objectsToDelete The objects to delete
*/
static void deleteAll(Object... objectsToDelete) {
currentGormStaticApi().deleteAll objectsToDelete
}
/**
* Deletes a list of objects in one go
* @param objectsToDelete Collection of objects to delete
*/
static void deleteAll(Iterable objectToDelete) {
currentGormStaticApi().deleteAll objectToDelete
}
/**
* Creates an instance of this class
* @return The created instance
*/
static D create() {
currentGormStaticApi().create()
}
/**
* Retrieves and object from the datastore. eg. Book.get(1)
*/
static D get(Serializable id) {
currentGormStaticApi().get id
}
/**
* Retrieves and object from the datastore. eg. Book.read(1)
*
* Since the datastore abstraction doesn't support dirty checking yet this
* just delegates to {@link #get(Serializable)}
*/
static D read(Serializable id) {
currentGormStaticApi().read id
}
/**
* Retrieves and object from the datastore as a proxy. eg. Book.load(1)
*/
static D load(Serializable id) {
currentGormStaticApi().load id
}
/**
* Retrieves and object from the datastore as a proxy. eg. Book.proxy(1)
*/
static D proxy(Serializable id) {
currentGormStaticApi().proxy id
}
/**
* Retrieve all the objects for the given identifiers
* @param ids The identifiers to operate against
* @return A list of identifiers
*/
static List<D> getAll(Iterable<Serializable> ids) {
currentGormStaticApi().getAll ids
}
/**
* Retrieve all the objects for the given identifiers
* @param ids The identifiers to operate against
* @return A list of identifiers
*/
static List<D> getAll(Serializable... ids) {
currentGormStaticApi().getAll ids
}
/**
* @return Synonym for {@link #list()}
*/
static List<D> getAll() {
list()
}
/**
* Creates a criteria builder instance
*/
static BuildableCriteria createCriteria() {
currentGormStaticApi().createCriteria()
}
/**
* Creates a criteria builder instance
*/
static withCriteria(@DelegatesTo(Criteria) Closure callable) {
currentGormStaticApi().withCriteria callable
}
/**
* Creates a criteria builder instance
*/
static withCriteria(Map builderArgs, @DelegatesTo(Criteria) Closure callable) {
currentGormStaticApi().withCriteria builderArgs, callable
}
/**
* Locks an instance for an update
* @param id The identifier
* @return The instance
*/
static D lock(Serializable id) {
currentGormStaticApi().lock id
}
/**
* Merges an instance with the current session
* @param d The object to merge
* @return The instance
*/
static D merge(D d) {
currentGormStaticApi().merge d
}
/**
* Counts the number of persisted entities
* @return The number of persisted entities
*/
static Integer count() {
currentGormStaticApi().count()
}
/**
* Same as {@link #count()} but allows property-style syntax (Foo.count)
*/
static Integer getCount() {
currentGormStaticApi().getCount()
}
/**
* Checks whether an entity exists
*/
static boolean exists(Serializable id) {
currentGormStaticApi().exists id
}
/**
* Lists objects in the datastore. eg. Book.list(max:10)
*
* @param params Any parameters such as offset, max etc.
* @return A list of results
*/
static List<D> list(Map params) {
currentGormStaticApi().list params
}
/**
* List all entities
*
* @return The list of all entities
*/
static List<D> list() {
currentGormStaticApi().list()
}
/**
* The same as {@link #list()}
*
* @return The list of all entities
*/
static List<D> findAll(Map params = Collections.emptyMap()) {
currentGormStaticApi().findAll params
}
/**
* Finds an object by example
*
* @param example The example
* @return A list of matching results
*/
static List<D> findAll(D example) {
currentGormStaticApi().findAll example
}
/**
* Finds an object by example using the given arguments for pagination
*
* @param example The example
* @param args The arguments
*
* @return A list of matching results
*/
static List<D> findAll(D example, Map args) {
currentGormStaticApi().findAll example, args
}
/**
* Finds the first object using the natural sort order
*
* @return the first object in the datastore, null if none exist
*/
static D first() {
currentGormStaticApi().first()
}
/**
* Finds the first object sorted by propertyName
*
* @param propertyName the name of the property to sort by
*
* @return the first object in the datastore sorted by propertyName, null if none exist
*/
static D first(String propertyName) {
currentGormStaticApi().first propertyName
}
/**
* Finds the first object. If queryParams includes 'sort', that will
* dictate the sort order, otherwise natural sort order will be used.
* queryParams may include any of the same parameters that might be passed
* to the list(Map) method. This method will ignore 'order' and 'max' as
* those are always 'asc' and 1, respectively.
*
* @return the first object in the datastore, null if none exist
*/
static D first(Map queryParams) {
currentGormStaticApi().first queryParams
}
/**
* Finds the last object using the natural sort order
*
* @return the last object in the datastore, null if none exist
*/
static D last() {
currentGormStaticApi().last()
}
/**
* Finds the last object sorted by propertyName
*
* @param propertyName the name of the property to sort by
*
* @return the last object in the datastore sorted by propertyName, null if none exist
*/
static D last(String propertyName) {
currentGormStaticApi().last propertyName
}
/**
* Handles static method missing for dynamic finders
*
* @param methodName The name of the method
* @param arg the argument to the method
* @return The value
*/
static Object staticMethodMissing(String methodName, arg) {
currentGormStaticApi().methodMissing(methodName, arg)
}
/**
* Handles property missing, does nothing by default, sub traits to override
*
* @param property The property
* @return The value if an exception if the property doesn't exist
*/
static Object staticPropertyMissing(String property) {
try {
currentGormStaticApi().propertyMissing(property)
} catch (IllegalStateException e) {
throw new MissingPropertyException(property, this)
}
}
/**
* Handles property missing, does nothing by default, sub traits to override
*
* @param property The property
* @param value The value of the property
* @return The value if an exception if the property doesn't exist
*/
static void staticPropertyMissing(String property, value) {
try {
currentGormStaticApi().propertyMissing(property, value)
} catch (IllegalStateException e) {
throw new MissingPropertyException(property, this)
}
}
/**
* Finds the last object. If queryParams includes 'sort', that will
* dictate the sort order, otherwise natural sort order will be used.
* queryParams may include any of the same parameters that might be passed
* to the list(Map) method. This method will ignore 'order' and 'max' as
* those are always 'asc' and 1, respectively.
*
* @return the last object in the datastore, null if none exist
*/
static D last(Map queryParams) {
currentGormStaticApi().last queryParams
}
/**
* Finds all results matching all of the given conditions. Eg. Book.findAllWhere(author:"Stephen King", title:"The Stand")
*
* @param queryMap The map of conditions
* @return A list of results
*/
static List<D> findAllWhere(Map queryMap) {
currentGormStaticApi().findAllWhere queryMap
}
/**
* Finds all results matching all of the given conditions. Eg. Book.findAllWhere(author:"Stephen King", title:"The Stand")
*
* @param queryMap The map of conditions
* @param args The Query arguments
*
* @return A list of results
*/
static List<D> findAllWhere(Map queryMap, Map args) {
currentGormStaticApi().findAllWhere queryMap, args
}
/**
* Finds an object by example
*
* @param example The example
* @return A list of matching results
*/
static D find(D example) {
currentGormStaticApi().find example
}
/**
* Finds an object by example using the given arguments for pagination
*
* @param example The example
* @param args The arguments
*
* @return A list of matching results
*/
static D find(D example, Map args) {
currentGormStaticApi().find example, args
}
/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand")
*
* @param queryMap The map of conditions
* @return A single result
*/
static D findWhere(Map queryMap) {
currentGormStaticApi().findWhere queryMap
}
/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand")
*
* @param queryMap The map of conditions
* @param args The Query arguments
*
* @return A single result
*/
static D findWhere(Map queryMap, Map args) {
currentGormStaticApi().findWhere queryMap, args
}
/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand"). If
* a matching persistent entity is not found a new entity is created and returned.
*
* @param queryMap The map of conditions
* @return A single result
*/
static D findOrCreateWhere(Map queryMap) {
currentGormStaticApi().findOrCreateWhere queryMap
}
/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand"). If
* a matching persistent entity is not found a new entity is created, saved and returned.
*
* @param queryMap The map of conditions
* @return A single result
*/
static D findOrSaveWhere(Map queryMap) {
currentGormStaticApi().findOrSaveWhere queryMap
}
/**
* Execute a closure whose first argument is a reference to the current session.
*
* @param callable the closure
* @return The result of the closure
*/
static <T> T withSession(Closure<T> callable) {
currentGormStaticApi().withSession callable
}
/**
* Same as withSession, but present for the case where withSession is overridden to use the Hibernate session
*
* @param callable the closure
* @return The result of the closure
*/
static <T> T withDatastoreSession(Closure<T> callable) {
currentGormStaticApi().withDatastoreSession callable
}
/**
* Executes the closure within the context of a transaction, creating one if none is present or joining
* an existing transaction if one is already present.
*
* @param callable The closure to call
* @return The result of the closure execution
* @see #withTransaction(Map, Closure)
* @see #withNewTransaction(Closure)
* @see #withNewTransaction(Map, Closure)
*/
static <T> T withTransaction(Closure<T> callable) {
currentGormStaticApi().withTransaction callable
}
/**
* Executes the closure within the context of a new transaction
*
* @param callable The closure to call
* @return The result of the closure execution
* @see #withTransaction(Closure)
* @see #withTransaction(Map, Closure)
* @see #withNewTransaction(Map, Closure)
*/
static <T> T withNewTransaction(Closure<T> callable) {
currentGormStaticApi().withNewTransaction callable
}
/**
* Executes the closure within the context of a transaction which is
* configured with the properties contained in transactionProperties.
* transactionProperties may contain any properties supported by
* {@link org.springframework.transaction.support.DefaultTransactionDefinition}.
*
* <blockquote>
* <pre>
* SomeEntity.withTransaction([propagationBehavior: TransactionDefinition.PROPAGATION_REQUIRES_NEW,
* isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ]) {
* // ...
* }
* </pre>
* </blockquote>
*
* @param transactionProperties properties to configure the transaction properties
* @param callable The closure to call
* @return The result of the closure execution
* @see org.springframework.transaction.support.DefaultTransactionDefinition
* @see #withNewTransaction(Closure)
* @see #withNewTransaction(Map, Closure)
* @see #withTransaction(Closure)
*/
static <T> T withTransaction(Map transactionProperties, Closure<T> callable) {
currentGormStaticApi().withTransaction transactionProperties, callable
}
/**
* Executes the closure within the context of a new transaction which is
* configured with the properties contained in transactionProperties.
* transactionProperties may contain any properties supported by
* {@link org.springframework.transaction.support.DefaultTransactionDefinition}. Note that if transactionProperties
* includes entries for propagationBehavior or propagationName, those values
* will be ignored. This method always sets the propagation level to
* TransactionDefinition.REQUIRES_NEW.
*
* <blockquote>
* <pre>
* SomeEntity.withNewTransaction([isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ]) {
* // ...
* }
* </pre>
* </blockquote>
*
* @param transactionProperties properties to configure the transaction properties
* @param callable The closure to call
* @return The result of the closure execution
* @see org.springframework.transaction.support.DefaultTransactionDefinition
* @see #withNewTransaction(Closure)
* @see #withTransaction(Closure)
* @see #withTransaction(Map, Closure)
*/
static <T> T withNewTransaction(Map transactionProperties, Closure<T> callable) {
currentGormStaticApi().withNewTransaction transactionProperties, callable
}
/**
* Executes the closure within the context of a transaction for the given {@link org.springframework.transaction.TransactionDefinition}
*
* @param callable The closure to call
* @return The result of the closure execution
*/
static <T> T withTransaction(TransactionDefinition definition, Closure<T> callable) {
currentGormStaticApi().withTransaction definition, callable
}
/**
* Creates and binds a new session for the scope of the given closure
*/
static <T> T withNewSession(Closure<T> callable) {
currentGormStaticApi().withNewSession callable
}