grails DefaultGrailsDomainClass.getIdentifier always returns 'id 作为标识符
grails DefaultGrailsDomainClass.getIdentifier always returns 'id as identifier
我正在尝试为基于 angular 的 CRUD 应用程序编写生成器(脚手架,但在客户端,使用 angular)
其中一部分是从 GORM 中提取元信息
现在,我显然碰壁了。
我想处理ID字段不是生成的,而是由应用程序管理的情况。
假设我有一个 class 定义,例如:
@Resource(readOnly = false, formats = ['json', 'xml'])
class Phone {
int age
String phoneId
String imageUrl
String name
String snippet
static mapping = {
id name: 'phoneId', generator: 'assigned'
}
static constraints = {
snippet(nullable: true, maxSize: 2000)
}
}
我想检索用作此 class 标识符的字段,此处应为 'phoneId'。
但是如果我要求:
grailsApplication.getDomainClass(com.phonecat.Phone.getName()).identifier
我得到的是 属性,称为 'id',类型为 Long:
DefaultGrailsDomainClassProperty@6e59cb9b name = 'id', type = Long, persistent = true, optional = false, association = false, bidirectional = false, association-type = [null]]
我是不是误用了 getIdentifier 方法?
我错过了什么吗?
还是我在 Grails/GORM 中遇到了错误?
为了完整起见,这是我正在使用的 GORM 版本(据我所知......最近在 gorm 方面发生了很多变化......):
来自 build.groovy:
compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.1.2.Final"
compile "org.hibernate:hibernate-ehcache:5.1.2.Final"
此外,我正在使用 grails 控制台插件 运行 这些测试:
runtime 'org.grails.plugins:grails-console:2.0.6'
(我觉得非常棒)
但在正常的命令行控制台中可能 运行 也是一样的。
这是我使用的代码:
import com.phonecat.*
println grailsApplication.getDomainClass(Phone.getName()).identifier
def d = grailsApplication.getDomainClass(Phone.getName())
println "${d.class} ${d.clazz.name} ${d.identifier}"
def pd = Phone.findAll().each {c ->
println "${c.id} ${c.phoneId}"
}
null
问题是:假设(实际上恰好是...)我正在编写一个插件来检索有关域的元信息class;我如何从 GORM 中获取此 class 已分配字段 'phoneId' 作为唯一标识符的信息,这是我在通过 REST 查询资源时应该查看的值?
如果您想检查模型的映射,通常您应该使用 GORM API 而不是 GrailsDomainClass。为此,您需要获得对 MappingContext 的引用(它可以由 Spring 依赖注入)。那么:
PersistentEntity entity = mappingContext.getPersistentEntity(Phone.name)
println entity.identity.name
我正在尝试为基于 angular 的 CRUD 应用程序编写生成器(脚手架,但在客户端,使用 angular)
其中一部分是从 GORM 中提取元信息 现在,我显然碰壁了。
我想处理ID字段不是生成的,而是由应用程序管理的情况。 假设我有一个 class 定义,例如:
@Resource(readOnly = false, formats = ['json', 'xml'])
class Phone {
int age
String phoneId
String imageUrl
String name
String snippet
static mapping = {
id name: 'phoneId', generator: 'assigned'
}
static constraints = {
snippet(nullable: true, maxSize: 2000)
}
}
我想检索用作此 class 标识符的字段,此处应为 'phoneId'。
但是如果我要求:
grailsApplication.getDomainClass(com.phonecat.Phone.getName()).identifier
我得到的是 属性,称为 'id',类型为 Long:
DefaultGrailsDomainClassProperty@6e59cb9b name = 'id', type = Long, persistent = true, optional = false, association = false, bidirectional = false, association-type = [null]]
我是不是误用了 getIdentifier 方法? 我错过了什么吗? 还是我在 Grails/GORM 中遇到了错误?
为了完整起见,这是我正在使用的 GORM 版本(据我所知......最近在 gorm 方面发生了很多变化......):
来自 build.groovy:
compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.1.2.Final"
compile "org.hibernate:hibernate-ehcache:5.1.2.Final"
此外,我正在使用 grails 控制台插件 运行 这些测试:
runtime 'org.grails.plugins:grails-console:2.0.6'
(我觉得非常棒) 但在正常的命令行控制台中可能 运行 也是一样的。
这是我使用的代码:
import com.phonecat.*
println grailsApplication.getDomainClass(Phone.getName()).identifier
def d = grailsApplication.getDomainClass(Phone.getName())
println "${d.class} ${d.clazz.name} ${d.identifier}"
def pd = Phone.findAll().each {c ->
println "${c.id} ${c.phoneId}"
}
null
问题是:假设(实际上恰好是...)我正在编写一个插件来检索有关域的元信息class;我如何从 GORM 中获取此 class 已分配字段 'phoneId' 作为唯一标识符的信息,这是我在通过 REST 查询资源时应该查看的值?
如果您想检查模型的映射,通常您应该使用 GORM API 而不是 GrailsDomainClass。为此,您需要获得对 MappingContext 的引用(它可以由 Spring 依赖注入)。那么:
PersistentEntity entity = mappingContext.getPersistentEntity(Phone.name)
println entity.identity.name