31

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.

1
  • To be a bit more explicit, persistentProperties only provides property names, not values. getProperties() is still necessary if you need both keys and values of properties. Confusing since Gorm is not consistent with its use of the term 'property'
    – Ed J
    Commented May 7, 2016 at 7:49

4 Answers 4

47

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.

4
  • Thanks for the quick response. I had tried that but I get the following exception: groovy.lang.MissingPropertyException: No such property: persistentProperties for class: com.infinitecampus.standards.LLevel. The LLevel object is a GORM object I created with new LLevel(). Perhaps I'm doing something else wrong?
    – lambmj
    Commented Dec 29, 2010 at 19:37
  • That's strange. What happens if you cast it to a GrailsDomainClass, e.g. ((GrailsDomainClass)domainObject).persistentProperties?
    – Rob Hruska
    Commented Dec 29, 2010 at 20:13
  • Tried it, here's what happened: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Name' with class 'com.infinitecampus.standards.LLevel' to class 'org.codehaus.groovy.grails.commons.GrailsDomainClass'
    – lambmj
    Commented Dec 29, 2010 at 20:25
  • After doing some more reading I updated my example. Give it a try.
    – Rob Hruska
    Commented Dec 29, 2010 at 21:37
13

Now (strarting Grails 2.x) you don't even have to instantiate new DefaultGrailsDomainClass(...) and avoid unnecessary code executions. All domain class objects have injected property domainClass:

def domainObject = new YourDomain()
domainObject.domainClass.persistentProperties

Or, if you haven't domain class object, you can get DefaultGrailsDomainClass from application context by domain class name - each domain class has a DefaultGrailsDomainClass registered as a Spring bean. So you can use, for example, Holders (assuming your domain class name is 'Foo'):

def defaultGrailsDomainClass = Holders.applicationContext.getBean("FooDomainClass")
defaultGrailsDomainClass.persistentProperties
1
  • 6
    I like this as a convenience on top of Rob's answer. However, the correct property name is actually persistentProperties, not persistedProperties. Commented Nov 14, 2014 at 20:58
9

As of grails 3.3.0

All code that uses the GrailsDomainClass or GrailsDomainClassProperty classes should be re-written to use the mapping context api.

To get started, inject the grailsDomainClassMappingContext bean. See the api documentation for more information on the MappingContext, PersistentEntity (GrailsDomainClass), and PersistentProperty(GrailsDomainClassProperty)

For example:

class MyService {
    def grailsDomainClassMappingContext //inject

    def accessDomainProperties(Class clazz) {
        PersistentEntity entityClass = grailsDomainClassMappingContext.getPersistentEntity(clazz.name)
        List<PersistentProperty> persistentPropertyList = entityClass.persistentProperties
        persistentPropertyList.each { property ->
            println property.name
        }
    }
}

Hope this helps someone.

6
  • This helped me, but is there a way to find out current property is allowed to be nullable or not? I have given constraint in the domain. I tried but didn't get proper values for constraint and its value. I want to get constraint and it value
    – emphywork
    Commented Oct 9, 2017 at 10:50
  • 1
    If I'm not mistaken, I think you to check nullable using persistentProperty.isNullable() method. But you can't access value unless it's static propertey or a initiated under object. To access static property value you can use GrailsClassUtils.getStaticPropertyValue(clazz,prop) Commented Oct 10, 2017 at 1:53
  • above returns non transient objects to retun transient under grails 3.2+ def d = new DefaultGrailsDomainClass(clazz) List properties=d.persistentProperties.transients.sort().unique().flatten() with above answer gets all
    – V H
    Commented Dec 7, 2017 at 11:19
  • 1
    DefaultGrailsDomainClass class is deprecated, so I won't recommend using that. Commented Jan 10, 2018 at 2:02
  • @charu-jain persistentProperty.getAssociations() returns a list of the associations for this entity. This is typically a subset of the list returned by getPersistentProperties(). You can further check Association.owningSide to determine which side of the relation you are in. Commented Jul 3, 2018 at 9:25
0

From your domain it's possible to get the persistent properties calling first the static method getGormPersistentEntity():

def newMap = [:]
YourDomain.gormPersistentEntity.persistentProperties.each { property ->
    newMap.put("$property.name", domainObject."$property.name")
}

Another alternative is to use HibernateMappingContext:

Through grailsApplication

// Inject grailsApplication   
grailsApplication.mappingContext.getPersistentEntity(YourDomain.class.name).persistentProperties

or grailsDomainClassMappingContext

// Inject grailsDomainClassMappingContext 
grailsDomainClassMappingContext.getPersistentEntity(YourDomain.class.name).persistentProperties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.