I'm recreating a working Grails 2.2.5 application in Grails 4 in order to get to know the new version (with the view to migrating all 2.2.x apps over in due course). So far I've only moved a handful of Groovy classes from the src directory over, but I'm running into a compile problem with a class which is apparently no longer present in Grails 4, org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. I use this to iterate through the persistent properties of a domain class (with persistentProperties
). How would I do this in Grails 4? I.e., get all the persistent properties of a domain class?
Asked
Active
Viewed 842 times
2

John Moore
- 6,739
- 14
- 51
- 67
-
This was deprecated in grails 3.3.x (maybe earlier?). See http://docs.grails.org/3.3.2/guide/upgrading.html for info on the upgrade path. I don't have a working example for grails 4 yet, but in case no one else chimes in with an actual answer, that may at least get you pointed in the right direction. – Daniel Mar 23 '20 at 18:32
-
No mention of it in the upgrade guide, unfortunately. I believe the answer may be to use PersistentEntity, and I'm looking for some good examples online. – John Moore Mar 23 '20 at 18:47
-
The grails 3 upgrade guide does reference it, at least. Search the link above for "Grails Domain Class API Deprecated". There are some good links, though not necessarily examples. But yes, PersistentEntity, PersistenProperty, and an injected grailsDomainClassMappingContext are the way to go. – Daniel Mar 23 '20 at 22:09
-
I am using this in a straight Groovy class in the /src directory, so I imagine I can't inject the grailsDomainClassMappingContext there, can I? – John Moore Mar 24 '20 at 09:18
-
You should be able to wire beans into a groovy class. Take a look at scaffolding for some examples of using persistentProperties https://github.com/grails/scaffolding/blob/master/core/src/main/groovy/org/grails/scaffolding/model/property/DomainPropertyImpl.groovy – erichelgeson Mar 24 '20 at 13:42
2 Answers
3
DefaultGrailsDomainClass
is indeed deprecated since Grails 3.3.2 in favor of the mapping context API. Fortunately, it is quite straightforward to replace the deprecated implementation.
Inject the grailsDomainClassMappingContext
bean in your service or controller:
def grailsDomainClassMappingContext
then get the persistent entity by providing its name:
def entity = grailsDomainClassMappingContext.getPersistentEntity(domainObjName)
where domainObjName
is a string and entity
is an instance of org.grails.datastore.mapping.model.PersistentEntity
. You can also get a specific property by using:
def property = entity.getPropertyByName(propertyName)
where propertyName
is a string and property
is an instance of org.grails.datastore.mapping.model.PersistentProperty
.
The interfaces PersistentEntity
and PersistentProperty
offer a variety of useful methods to cover many uses.

Hildeberto Mendonca
- 133
- 1
- 6
0
Read my answer on this other thread:
1.
YourDomain.gormPersistentEntity.persistentProperties
2. Inject grailsApplication
grailsApplication.mappingContext.getPersistentEntity(YourDomain.class.name).persistentProperties
3. Inject grailsDomainClassMappingContext
grailsDomainClassMappingContext.getPersistentEntity(YourDomain.class.name).persistentProperties

Maicon Mauricio
- 2,052
- 1
- 13
- 29