Grails GORM:如何创建复合主键并将其用于表关系? [英] Grails GORM: How do I create a composite primary key and use it for a table relationship?
问题描述
我有两个表,其中一个(旧表:A)有两个字段应该用作复合外键,另一个(新表:B)应该使用复合主键作为 每个row:A 有一个 row:B
关系.我如何用 GORM 来描述这些表?
I have two tables, one of which (legacy table: A) has two fields that should serve as a composite foreign key and the other one (new table: B) should use a composite primary key for a each row:A has one row:B
relationship. How do I describe these tables in terms of GORM?
到目前为止,我已经能够创建一个反映旧表的域类:A
So far I've been able to create a domain class that reflects the legacy table:A
class A {
...
//composite foreign key to link B class
String className;
String eventName;
B b; //instance of B to be related
static mapping = {
table 'a_table';
id column: 'id';
className column: 'class_name';
eventName column: 'event_name';
//b: ???
}
}
哪个有效,但我不能创建一个 new class:B
和关系.
which works, but I can't create a new class:B
and the relationship.
我试图将 B 声明为:
I tried to declare B as:
class B implements Serializable{
static auditable = true;
String name;
String className;
String eventName;
static mapping = {
//supposed to make a composite PK
id composite:[className, eventName]
}
}
但这不会用
编译ERROR context.GrailsContextLoader - Error execution bootstraps: Error Evaluation ORM mappings block for domain [com.package.B]: No such property: eventName for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder
我想要的是:
static mapping = {
...
b composite: [b.className:className, b.eventName:eventName]
//or whatever is the right way for this to be done.
}
A 类让 GORM 处理这个关系.
for the A class to make GORM handle this relation.
推荐答案
您是否尝试使用属性名称而不是使用属性值?
Did you try to use attribute name instead of use attribute value ?
class B implements Serializable{
String name;
String className;
String eventName;
static mapping = {
//supposed to make a composite PK
id composite:['className', 'eventName']
}
}
A 中的映射:
class A {
static hasMany = [ b : B ]
}
A
这篇关于Grails GORM:如何创建复合主键并将其用于表关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!