I need to get all the information about a particular Grails domain class, i.e. the persistent properties and the constraints related to them in a single collection. How do I do that?
As of grails 3.3, the accepted answer is no longer correct. DefaultGrailsDomainClass
is now deprecated and the single argument constructor will throw an Exception that mappingContext is not yet initialized.
Inject the grailsDomainClassMappingContext
bean and get the PersistentEntity
with
def persistentEntity = grailsDomainClassMappingContext.getPersistentEntity(MyDomain.class.name)
def propertyList = persistentEntity.getPersistentProperties()
The following with get you a map with the property name as key and a map of constraints as values, works in Grails 3:
def d = new DefaultGrailsDomainClass(MyDomain.class)
def pp = d.persistentProperties.collectEntries {
[it.name, d.constrainedProperties[it.name]?.appliedConstraints ]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With