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

时间:2012-02-22 01:39:25

标签: grails

我在Grails工作。我正在编写一个代码,用于在单击“删除”链接后删除特定登录用户的帐户。现在这是我在控制器中找到的代码,

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的新手。

1 个答案:

答案 0 :(得分:2)

如果您有一组项目而不是一个项目,请使用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])