【发布时间】:2011-10-20 07:35:17
【问题描述】:
我有一个关于在 grails 中创建关联表以协调多对多关系的问题。设置是这样的: 1. Domain A(Client Profile)可以有多个 Domain B(Friends) 2.每个域B(朋友)可以有多个域A(客户档案) 3. 为了解决这个问题,我需要创建一个关联表(或域),其中包含来自每个表的 FK。此域可以命名为域 C (client_friend)
这是我目前的代码:
class DomainA{
String id
String firstName
String lastName
static hasMany = [domainB: DomainB]
static mapping = {
cache true
id generator: 'assigned'
columns {
firstName type:'text'
lastName type:'text'
alumConnections column: 'domaina_id', joinTable: 'a_b'
}
}
static constraints = {
firstName (nullable:true)
lastName (nullable:true)
}
}
DomainB 代码:
class DomainB{
String id
String firstName
String lastName
static hasMany = [domainA:DomainA]
static belongsTo = DomainA
static mapping = {
cache true
id generator: 'assigned'
columns {
firstName type:'text'
lastName type:'text'
domainA column: 'domainb_id', joinTable: 'a_b'
}
}
static constraints = {
firstName (nullable:true)
lastName (nullable:true)
}
}
域 A_B 代码:
class AB{
Date dateCreated
Date lastUpdated
long version
}
当我运行这段代码时,它似乎可以工作。创建了使用 MySQL 的表,FK 似乎就位。当我在 DomainB 类中输入数据时,数据被输入,并且来自 DomainA 和 DomainB 的 PK 都被插入到 A_B 中。但是,当我尝试从 A_B 中删除值时,问题就来了。我尝试过这样的事情:
AB results =AB.findByAIdAndBId('jIi-hRi4cI','2BYvuA2X14')
但出现错误:InvalidPropertyException: No property found for name [a_id] for class [class mgr.AB]
我的问题是:首先,我的设置是否正确?其次,如果是这样,那么我如何查询AB表谁的PK是由DomainA和DomainB组成的?
感谢您的帮助。
杰森
【问题讨论】:
标签: hibernate grails grails-domain-class grails-controller