gpt4 book ai didi

grails - 如何从不同的域类中删除数据?

转载 作者:行者123 更新时间:2023-12-02 15:13:36 28 4
gpt4 key购买 nike

我在Grails工作。我正在编写一个代码,一旦他单击“删除”链接,便删除该特定登录用户的帐户。现在,这是我在 Controller 中找到的代码,

def delete = {
def account = springSecurityService.getCurrentUser()
def registrant = Registrant.findByAccount(account)
def loggeduser = registrant.account.username

RegistrantEligibilityInformation.executeUpdate("delete RegistrantEligibilityInformation as rei where rei.registrant in (select reg from Registrant as reg where reg.account.username in(:loggeduser))",[loggeduser:loggeduser])
RegistrantEducationInformation.executeUpdate("delete RegistrantEducationInformation as reduc where reduc.registrant in (select reg from Registrant as reg where reg.account.username in(:loggeduser))",[loggeduser:loggeduser])
Registrant.executeUpdate("delete Registrant as reg where reg.account.username in(:loggeduser)",[loggeduser:loggeduser])
AccountRole.executeUpdate("delete AccountRole as actrole where actrole.account.username in(:loggeduser)",[loggeduser:loggeduser])
Account.executeUpdate("delete Account as act where act.username in(:loggeduser)",[loggeduser:loggeduser])

toolsService.deletion(registrant)
}

我这里有五个域类,分别是RegistrantEligibilityInformation,RegistrantEducationInformation,Registrant,AccountRole和Account。每次运行程序时,都会出现如下错误消息:
URI: /user/profile/delete
Class: org.postgresql.util.PSQLException
Message: ERROR: column reference "id" is ambiguous Position: 49

...与此代码有关:
RegistrantEligibilityInformation.executeUpdate("delete RegistrantEligibilityInformation as rei where rei.registrant in (select reg from Registrant as reg where reg.account.username in(:loggeduser))",[loggeduser:loggeduser])

...而且我认为其余的代码也是如此。我不知道该怎么办,因为我还是Grails的新手。

最佳答案

如果您有一组项目,请使用in而不是一个项目-必须匹配时使用=。因此,in对子查询有意义,但loggeduser不匹配。

我不确定为什么Hibernate不支持您正在使用的子查询,但是如果您在id而不是整个对象上进行匹配,它就可以工作:

RegistrantEligibilityInformation.executeUpdate(
"delete RegistrantEligibilityInformation as rei where rei.registrant.id in (" +
"select reg.id from Registrant as reg where reg.account.username=:loggeduser)",
[loggeduser:loggeduser])

RegistrantEducationInformation.executeUpdate(
"delete RegistrantEducationInformation as reduc where reduc.registrant.id in (" +
"select reg.id from Registrant as reg where reg.account.username=:loggeduser)",
[loggeduser:loggeduser])

Registrant.executeUpdate(
"delete Registrant as reg where reg.account.username=:loggeduser",
[loggeduser:loggeduser])

AccountRole.executeUpdate(
"delete AccountRole as actrole where actrole.account.username=:loggeduser)",
[loggeduser:loggeduser])

Account.executeUpdate(
"delete Account as act where act.username=:loggeduser)",
[loggeduser:loggeduser])

关于grails - 如何从不同的域类中删除数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388008/

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