我有一个关于在 grails 中创建关联表以协调多对多关系的问题。设置是这样的: 1. 域 A(客户端配置文件)可以有多个域 B(朋友) 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)
}
}
域B代码:
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组成的?
谢谢你的帮助。
杰森