Grails 正在检索域的GORM持久属性列表
获取与给定GORM域对象关联的持久属性列表的最佳/最简单方法是什么?我可以获得所有属性的列表,但此列表包含非持久性字段,如Grails 正在检索域的GORM持久属性列表,grails,gorm,Grails,Gorm,获取与给定GORM域对象关联的持久属性列表的最佳/最简单方法是什么?我可以获得所有属性的列表,但此列表包含非持久性字段,如类和约束 目前,我正在使用它,并使用我创建的列表筛选出非持久属性的列表: def nonPersistent = ["log", "class", "constraints", "properties", "errors", "mapping", "metaClass"] def newMap = [:] domainObject.getPropert
类
和约束
目前,我正在使用它,并使用我创建的列表筛选出非持久属性的列表:
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
}
}
似乎应该有更好的方法来获取持久属性。试试以下方法:
import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass
...
def d = new DefaultGrailsDomainClass(YourDomain.class)
d.persistentProperties
这里有一个指向的GrailsAPI的链接(这是一个指向旧版本的链接;在快速搜索之后,我找不到新版本)。它有一个getPersistentProperties()
(在上面的代码片段中使用)。您可以遍历API文档,查看哪些其他方法可能对您有用
如果您想要一个示例,请执行grails安装模板
,然后查看src/templates/scaffolding/create.gsp
。其中有一个块,它在其中迭代持久域属性。现在(strarting Grails 2.x)您甚至不必实例化新的DefaultGrailDomainClass(…)
,并避免不必要的代码执行。所有域类对象都已注入属性domainClass
:
def domainObject = new YourDomain()
domainObject.domainClass.persistentProperties
或者,如果您没有域类对象,您可以通过域类名称从应用程序上下文中获取DefaultGrailDomainClass
——每个域类都有一个注册为Springbean的DefaultGrailDomainClass。因此,您可以使用,例如,持有者
(假设您的域名类名为“Foo”):
从grails3.3.0开始
所有使用GrailDomainClass
或GrailDomainClassProperty
类的代码都应该重新编写以使用映射上下文api
首先,注入grailDomainClassMappingContext
bean。有关MappingContext
、PersistentEntity
(GrailDomainClass)和PersistentProperty
(GrailDomainClassProperty)的更多信息,请参阅api文档
例如:
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
}
}
}
classmyservice{
def GrailDomainClassMappingContext//inject
def accessDomainProperties(clazz类){
PersistentEntity entityClass=GrailDomainClassMappingContext.getPersistentEntity(clazz.name)
List persistentPropertyList=entityClass.persistentProperties
persistentPropertyList.each{property->
println property.name
}
}
}
希望这对某人有所帮助。感谢您的快速回复。我已经尝试过了,但是我得到了以下异常:groovy.lang.MissingPropertyException:没有这样的属性:类的persistentProperties:com.infinitecampus.standards.LLevel
。LLevel对象是我用new LLevel()创建的GORM对象。也许我做错了什么?真奇怪。如果将其强制转换为GrailDomainClass,例如((GrailDomainClass)domainObject),会发生什么情况。persistentProperties
?尝试过,发生的情况如下:org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法将类为“com.infinitecampus.standards.LLevel”的对象“Name”强制转换为类为“org.codehaus.groovy.grails.commons.GrailsDomainClass”
在阅读了更多的内容后,我更新了我的示例。试试看。我喜欢在罗布回答的基础上再加上这个方便。但是,正确的属性名实际上是persistentProperties,而不是persistendproperties。更明确一点,persistentProperties只提供属性名,而不是值。如果同时需要属性的键和值,则getProperties()仍然是必需的。令人困惑的是,由于Gorm和它对“属性”一词的使用不一致,这对我很有帮助,但有没有办法确定当前属性是否允许为空?我在域中给出了约束。我尝试了,但没有得到约束及其值的正确值。我想获取约束和它的值如果我没弄错的话,我想您应该使用persistentProperty.isNullable()
方法检查nullable。但您不能访问值,除非它是静态属性
或在对象下启动。要访问静态属性值,可以使用上面的GrailsClassUtils.getStaticPropertyValue(clazz,prop)
返回非瞬态对象,以在grails 3.2+def d=new defaultGrailDomainClass(clazz)列表属性=d.persistentProperties.transients.sort().unique()下重新运行transient
以上答案获取所有DefaultGrailDomainClass
类已弃用,所以我不建议使用该类。@charu jainpersistentProperty.getAssociations()
返回此实体的关联列表。这通常是getPersistentProperties()
返回的列表的子集。您可以进一步检查Association.owningSide
,以确定您处于关系的哪一方。
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
}
}
}