我正在 Grails 4 中重新创建一个有效的 Grails 2.2.5 应用程序,以便了解新版本(以期在适当的时候迁移所有 2.2.x 应用程序)。到目前为止,我只从 src 目录中移动了几个 Groovy 类,但是我遇到了一个编译问题,该类显然不再存在于 Grails 4 中,即 org.codehaus.groovy.grails.commons。默认GrailsDomainClass。我使用它来遍历域类的持久属性(使用 persistentProperties
)。我将如何在 Grails 4 中执行此操作?即,获取域类的所有持久属性?
最佳答案
DefaultGrailsDomainClass
自 Grails 3.3.2 以来确实已弃用支持映射上下文 API。幸运的是,替换已弃用的实现非常简单。
注入(inject)grailsDomainClassMappingContext
您的服务或 Controller 中的 bean:
def grailsDomainClassMappingContext
然后通过提供其名称来获取持久实体:def entity = grailsDomainClassMappingContext.getPersistentEntity(domainObjName)
在哪里 domainObjName
是一个字符串和 entity
是 org.grails.datastore.mapping.model.PersistentEntity
的一个实例.您还可以使用以下方法获取特定属性:def property = entity.getPropertyByName(propertyName)
在哪里 propertyName
是一个字符串和 property
是 org.grails.datastore.mapping.model.PersistentProperty
的一个实例.接口(interface)
PersistentEntity
和 PersistentProperty
提供多种有用的方法来涵盖多种用途。
关于Grails 4 替代 DefaultGrailsDomainClass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60819262/