Retrieving a list of GORM persistent properties for a domain

前端 未结 3 1719
鱼传尺愫
鱼传尺愫 2020-12-08 00:47

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 cont

3条回答
  •  太阳男子
    2020-12-08 01:20

    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 persistentPropertyList = entityClass.persistentProperties
            persistentPropertyList.each { property ->
                println property.name
            }
        }
    }
    

    Hope this helps someone.

提交回复
热议问题