Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the persistent properties of a grails Domain Class along with their constraints in a single Collection?

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?

like image 478
Shashank Avatar asked Mar 25 '17 07:03

Shashank


2 Answers

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()
like image 165
Trebla Avatar answered Oct 06 '22 21:10

Trebla


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 ]
}
like image 31
Mike W Avatar answered Oct 06 '22 20:10

Mike W