Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a list of GORM persistent properties for a domain

What's the best/easiest way to get a list of the persistent properties associated with a given GORM domain object? I can get the list of all properties, but this list contains non-persistent fields such as class and constraints.

Currently I'm using this and filtering out the list of nonPersistent properties using a list I created:

    def nonPersistent = ["log", "class", "constraints", "properties", "errors", "mapping", "metaClass"]     def newMap = [:]     domainObject.getProperties().each { property ->         if (!nonPersistent.contains(property.key)) {             newMap.put property.key, property.value         }     } 

There seems like there must be a better way of getting just the persistent properties.

like image 994
lambmj Avatar asked Dec 29 '10 14:12

lambmj


1 Answers

Try this:

import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass ... def d = new DefaultGrailsDomainClass(YourDomain.class) d.persistentProperties 

Here's a link to the Grails API for GrailsDomainClass (it's a link to an older version; I couldn't find a newer one after some quick searches). It's got a getPersistentProperties() (used in the code snippet above). You can traverse the API documentation to see what other methods might be useful to you.

If you want an example, do a grails install-templates and then look at src/templates/scaffolding/create.gsp. There's a block in there where it iterates over the persistent domain properties.

like image 144
Rob Hruska Avatar answered Sep 30 '22 05:09

Rob Hruska