gpt4 book ai didi

grails - 使用joinTable进行一对多映射

转载 作者:行者123 更新时间:2023-12-02 13:50:08 25 4
gpt4 key购买 nike

我有两个域类。一个是“合作伙伴”,另一个是“客户”。客户可以是合作伙伴的一部分,并且合作伙伴可以拥有1个或多个客户:

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。

有人知道我在做什么错吗?

编辑:我忘了提到我正在使用旧数据库表执行此操作。因此,我有一个Partner,Customer和PartnerMap表。 PartnerMap仅具有一个customer_id和partner_id字段。

最佳答案

考虑到当您想要一个联接表时“一对多”工作的方式,我认为使用标准GORM不可能使其成为双向并访问客户的合作伙伴。但是您可以将联接表与域类映射,并以这种方式访问​​内容:

顾客:

class Customer {
String name
def getPartner() {
PartnerMap.findByCustomer(this)?.partner
}
}

伙伴:
class Partner {
String name
def getCustomers() {
PartnerMap.findAllByPartner(this)*.customer
}
}

合作伙伴 map :
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()来关联两个实例。您还会丢失域类中的集合和反向引用,但是我为它们添加了实用程序方法。

关于grails - 使用joinTable进行一对多映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com