GORM - 获取域 class 属性的原始数据库值
GORM - get raw DB value for domain class properties
我在我的 Grails 3 网络应用程序中使用 MongoDB 的 GORM 来管理来自数据库的 read/writes。
我有以下 2 个域 classes:
class Company {
String id
}
class Team {
String id
Company company
}
对于团队,他们的公司作为字符串保存在数据库中,使用 GORM 我可以简单地使用 team.company
来获取 Company
域 class.[=19= 的实例]
但是,我需要覆盖 company
的 getter,并且我需要公司 ID 的原始值(存储在数据库中),而 GORM 不会妨碍并发挥其魔力.
有没有办法获取原始字符串值?
欢迎任何帮助!提前致谢
更新(5 月 27 日)
调查@TaiwaneseDavidCheng 的建议,我将代码更新为
class Company {
String id
}
class Team {
String id
Company company
String companyId
static mapping = {
company attr: "company" // optional
companyId attr: "company", insertable: false, updateable: false
}
}
请注意,我正在为 MongoDB 使用 GORM,它(引用 manual)tries to be as compatible as possible with GORM for Hibernate
,但需要稍微不同的实现。
但是我发现(通过反复试验)MongoDB 的 GORM 不支持类似的解决方案,因为它似乎一次只能将一个 属性 映射到 MongoDB 文档 属性.
特别是按字母顺序排列的最后 属性 获胜,例如companyId
在我的示例中。
我想出了一个让整个事情正常进行的方法,我在下面发布了我自己的答案。
给定域 class
中的 non-insertable non-updateable 列 "companyId"
class Company {
String id
}
class Team {
String id
Company company
Long companyId
static mapping = {
company column:"companyId"
companyId column:"companyId",insertable: false,updateable: false
}
}
(按照上面对我的问题的编辑)
我定义了一个自定义映射,并通过为团队的公司定义自定义 getter 和 setter 来使用 Grails transients。
class Company {
String id
}
class Team {
String id
Company company
String companyId
static mapping = {
companyId attr: "company" // match against MongoDB property
}
static transients = [ 'company' ] // non-persistent property
Company getCompany() {
return Company.get(companyId)
}
void setCompany(Company company) {
companyId = company.id
}
}
我在我的 Grails 3 网络应用程序中使用 MongoDB 的 GORM 来管理来自数据库的 read/writes。
我有以下 2 个域 classes:
class Company {
String id
}
class Team {
String id
Company company
}
对于团队,他们的公司作为字符串保存在数据库中,使用 GORM 我可以简单地使用 team.company
来获取 Company
域 class.[=19= 的实例]
但是,我需要覆盖 company
的 getter,并且我需要公司 ID 的原始值(存储在数据库中),而 GORM 不会妨碍并发挥其魔力.
有没有办法获取原始字符串值?
欢迎任何帮助!提前致谢
更新(5 月 27 日)
调查@TaiwaneseDavidCheng 的建议,我将代码更新为
class Company {
String id
}
class Team {
String id
Company company
String companyId
static mapping = {
company attr: "company" // optional
companyId attr: "company", insertable: false, updateable: false
}
}
请注意,我正在为 MongoDB 使用 GORM,它(引用 manual)tries to be as compatible as possible with GORM for Hibernate
,但需要稍微不同的实现。
但是我发现(通过反复试验)MongoDB 的 GORM 不支持类似的解决方案,因为它似乎一次只能将一个 属性 映射到 MongoDB 文档 属性.
特别是按字母顺序排列的最后 属性 获胜,例如companyId
在我的示例中。
我想出了一个让整个事情正常进行的方法,我在下面发布了我自己的答案。
给定域 class
中的 non-insertable non-updateable 列 "companyId"class Company {
String id
}
class Team {
String id
Company company
Long companyId
static mapping = {
company column:"companyId"
companyId column:"companyId",insertable: false,updateable: false
}
}
(按照上面对我的问题的编辑)
我定义了一个自定义映射,并通过为团队的公司定义自定义 getter 和 setter 来使用 Grails transients。
class Company {
String id
}
class Team {
String id
Company company
String companyId
static mapping = {
companyId attr: "company" // match against MongoDB property
}
static transients = [ 'company' ] // non-persistent property
Company getCompany() {
return Company.get(companyId)
}
void setCompany(Company company) {
companyId = company.id
}
}