Retrieving a list of GORM persistent properties for a domain

╄→гoц情女王★ 提交于 2019-11-27 00:28:12

问题


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:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/4555150/retrieving-a-list-of-gorm-persistent-properties-for-a-domain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!