在 GORM 中对对象属性使用非主键?
是否可以有一个域对象,其中的字段引用另一个对象上的非主键(我正在处理旧数据库)。
我拥有的是 flea_name
而不是 flea_id
(这是主键)。有没有办法让 GORM 根据非主唯一键(例如名称、代码等)而不是其 id 自动从 Flea 表中提取正确的 Flea 对象? 如:
class Flea {
def id //This is the primary key in the table
def name //This is a unique key
}
class Puppy {
def flea //This is stored as the flea's unique, but non-primary,
// name in the puppy table
//The puppy table has a flea_name, not a flea_id, as would be expected
static mapping = {
flea column: 'flea_name'//This will point to the flea's name,
//but I want to hydrate it to be a Flea
}
}
提前致谢
Is it possible to have a domain object with a field that references a non-primary key on another object (I am working on a legacy database).
What I have is the flea_name
not the flea_id
(which is the primary key). Is there a way to have GORM automatically pull the proper Flea object from the flea table based on a non-primary unique key (such as name, code, etc.), rather than its id?
Such as:
class Flea {
def id //This is the primary key in the table
def name //This is a unique key
}
class Puppy {
def flea //This is stored as the flea's unique, but non-primary,
// name in the puppy table
//The puppy table has a flea_name, not a flea_id, as would be expected
static mapping = {
flea column: 'flea_name'//This will point to the flea's name,
//but I want to hydrate it to be a Flea
}
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简短的回答:你不这样做。
你让 grails 使用 id 来链接数据库中的对象。那么如果您需要访问跳蚤名称,您可以重写其 toString() 方法以返回跳蚤名称。或者您像控制器/服务或 gsps 中的任何其他属性一样访问该属性。
short answer: you don't do it this way.
you let grails use the id to link the objects in the database. then if you need to access the fleas name you can override its
toString()
method to return the fleas name. or you access that property like any other in controllers/services or gsps.您能详细说明一下您为什么要这样做吗?如果对象关系确实是“小狗有跳蚤”,那么您确实希望像平常一样通过 Id 进行链接。
但是,如果您真正的意思是小狗有很多跳蚤,那么它要么有单个跳蚤的集合,或者在这种情况下更有可能,您并不真正想要这里的关系,而是小狗有一个数字字段标识跳蚤的数量。
听起来怎么样?
Can you elaborate on why you'd want to do that? If the object relationship really is that a Puppy HAS A Flea, then you really want to link via the Id as normal.
However, if what you really mean is that a Puppy HAS MANY Fleas, then it would either have a collection of individual fleas, or more likely in this case, you don't really want a relationship here but rather Puppy has a numeric field identifying the number of fleas it has.
How does that sound?
您可能想看看 [自定义 ORM 映射][1]
示例:
P/s:抱歉,因为链接中有一些空间,它是: http://grails.org/doc/1.0.x/guide/single.html#5.5.2 自定义 ORM映射
[1]:http://grails.org/doc/ 1.0.x/guide/single.html#5.5.2 自定义 ORM 映射
You may want to have a look at [Custom ORM Mapping][1]
An example:
P/s: sorry because of the link has some space in it, it's : http://grails.org/doc/1.0.x/guide/single.html#5.5.2 Custom ORM Mapping
[1]: http://grails.org/doc/1.0.x/guide/single.html#5.5.2 Custom ORM Mapping
如果 Flea 一直以这种方式使用,换句话说,它不是一次性的。这样,Person 表也将 flea_name 作为外键,为什么不呢?
根据您所使用的 Hibernate/Grails 版本,您可能会失去对实际 id 列的访问权限。
或者,如果这是一次性情况,你可以改变 Puppy
If Flea is consistently used in this way, in other words it is not a one off. Such that the Person table also has flea_name as the foreign key why not
Depending on the version of Hibernate/Grails you are on, you might loose access to the actual id column.
Or if this is a one off case you could change Puppy