我有两个域类。一个是“合作伙伴”,另一个是“客户”。客户可以是合作伙伴的一部分,合作伙伴可以拥有一个或多个客户:
class Customer {
Integer id
String name
static hasOne = [partner:Partner]
static mapping = {
partner joinTable:[name:'PartnerMap',column:'partner_id',key:'customer_id']
}
}
class Partner {
Integer id
static hasMany = [customers:Customer]
static mapping = {
customers joinTable:[name:'PartnerMap',column:'customer_id',key:'partner_id']
}
}
但是,每当我试图查看客户是否是合作伙伴的一部分时,就像这样:
def customers = Customer.list()
customers.each {
if (it.partner) {
println "Partner!"
}
}
我收到以下错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select this_.customer_id as customer1_162_0_, this_.company as company162_0_, this_.display_name as display3_162_0_, this_.parent_customer_id as parent4_162_0_, this_.partner_id as partner5_162_0_, this_.server_id as server6_162_0_, this_.status as status162_0_, this_.vertical_market as vertical8_162_0_ from Customer this_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
看起来Grails认为partner_id是Customer查询的一部分,而不是......它位于PartnerMap表中,该表应该找到customer_id,然后从相应的partner_id获取Partner。 / p>
任何人都知道我做错了什么?
编辑:我忘了提到我正在使用旧数据库表执行此操作。所以我有一个Partner,Customer和PartnerMap表。 PartnerMap只有一个customer_id和partner_id字段。
答案 0 :(得分:5)
考虑到1-many的工作方式,当您需要连接表时,我认为使用标准GORM不可能使其成为双向并访问客户的合作伙伴。但是您可以使用域类映射连接表并以这种方式访问:
客户:
class Customer {
String name
def getPartner() {
PartnerMap.findByCustomer(this)?.partner
}
}
合作伙伴:
class Partner {
String name
def getCustomers() {
PartnerMap.findAllByPartner(this)*.customer
}
}
PartnerMap:
import org.apache.commons.lang.builder.HashCodeBuilder
class PartnerMap implements Serializable {
Partner partner
Customer customer
boolean equals(other) {
if (!(other instanceof PartnerMap)) {
return false
}
other.partner?.id == partner?.id &&
other.customer?.id == customer?.id
}
int hashCode() {
def builder = new HashCodeBuilder()
if (partner) builder.append(partner.id)
if (customer) builder.append(customer.id)
builder.toHashCode()
}
static PartnerMap get(long partnerId, long customerId) {
find 'from PartnerMap where partner.id=:partnerId and customer.id=:customerId',
[partnerId: partnerId, customerId: customerId]
}
static PartnerMap create(Partner partner, Customer customer, boolean flush = false) {
new PartnerMap(partner: partner, customer: customer).save(flush: flush, insert: true)
}
static boolean remove(Partner partner, Customer customer, boolean flush = false) {
PartnerMap instance = PartnerMap.findByPartnerAndCustomer(partner, customer)
instance ? instance.delete(flush: flush) : false
}
static void removeAll(Partner partner) {
executeUpdate 'DELETE FROM PartnerMap WHERE partner=:partner', [partner: partner]
}
static void removeAll(Customer customer) {
executeUpdate 'DELETE FROM PartnerMap WHERE customer=:customer', [customer: customer]
}
static mapping = {
id composite: ['customer', 'partner']
version false
table 'PartnerMap'
}
}
由于您没有使用hasMany,因此会丢失addToXXX动态方法,但您可以调用PartnerMap.create()
来关联两个实例。您还丢失了域类中的集合和反向引用,但我为这些添加了实用程序方法。