我有两个域类的单向一对多关系:
package net.peddn
class User {
static hasMany = [texts: Text]
String username
}
和
package net.peddn
class Text {
long creator
String title
static constraints = {
creator( nullable: true )
}
}
我的用户控制器如下所示:
package net.peddn
class UserController {
static scaffold = true
def delete() {
def userInstance = User.get(params.id)
userInstance.texts.each { text ->
text.creator = null
}
userInstance.delete(flush: true)
}
我的 BootStrap.groovy 看起来像这样:
import net.peddn.Text
import net.peddn.User
class BootStrap {
def init = { servletContext ->
User user = new User(username: "username")
user.save(flush: true)
Text text1 = new Text(title: "titel 1", creator: user.id)
Text text2 = new Text(title: "titel 2", creator: user.id)
user.addToTexts(text1)
user.addToTexts(text2)
user.save(flush: true)
}
def destroy = {
}
}
当我现在尝试删除我的用户时,我收到以下错误:
| Server running. Browse to http://localhost:8080/usertexts
| Error 2012-06-17 19:33:49,581 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - IllegalArgumentException occurred when processing request: [POST] /usertexts/user/index - parameters:
id: 1
_action_delete: Löschen
Stacktrace follows:
Message: null
Line | Method
->> 21 | doCall in net.peddn.UserController$_delete_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 19 | delete in net.peddn.UserController
| 1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 603 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 722 | run . . . in java.lang.Thread
如果我将代码更改为
text.creator = 0
在 UserController.groovy 中完美运行。
顺便说一句:我使用了这个域模型,因为我不希望在删除用户时删除 Text 对象。但我也想知道谁创建了 Text 对象。如果有人对此问题有更好的解决方案,请告诉我。
谢谢!彼得