Reputation: 7129
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?
Upvotes: 3
Views: 1124
Reputation: 3061
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
Upvotes: 0
Reputation: 133
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.
Upvotes: 3