我有两个表,其中一个(旧表:A)有两个字段应该用作复合外键,另一个(新表:B)应该使用复合主键来建立each row:A has one row:B
关系。我如何用 GORM 来描述这些表?
到目前为止,我已经能够创建一个反映遗留表的域类: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
和关系。
我试图将 B 声明为:
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 executing bootstraps: Error evaluating 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 处理这种关系。