[英]Grails GORM: How to 'null' a property of a child in a one-to-many relationship?
I have a undirectional one to many relationship of two domain classes: 我有两个域类的无定向一对多关系:
package net.peddn
class User {
static hasMany = [texts: Text]
String username
}
and 和
package net.peddn
class Text {
long creator
String title
static constraints = {
creator( nullable: true )
}
}
my User controller looks like this: 我的用户控制器如下所示:
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)
}
my BootStrap.groovy looks like this: 我的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 = {
}
}
When I now try to delete my User I get the following error: 现在,当我尝试删除我的用户时,出现以下错误:
| 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
If i change the code to 如果我将代码更改为
text.creator = 0
in UserController.groovy is works perfectly. 在UserController.groovy中是完美的作品。
By the way: I used this domain model because I don't want the Text objects to be deleted when a User will be deleted. 顺便说一句:我使用此域模型是因为我不希望在删除用户时删除Text对象。 But I also want to know who created the Text object.
但是我也想知道谁创建了Text对象。 If someone has a better solution for this problem PLEASE let me know.
如果有人对这个问题有更好的解决方案,请告诉我。
Thanks! 谢谢! Peter
彼得
With your current implementation please try if this works: 使用当前的实现,请尝试是否可行:
def delete() {
def userInstance = User.get(params.id)
userInstance.texts.each { text ->
userInstance.removeFromTexts(text)
}
userInstance.delete(flush: true)
}
If you want bidirectional relation User-Texts and you don't want texts to be deleted when user is deleted you can change this behaviour. 如果您想要双向关系用户文本,并且您不希望在删除用户时删除文本,则可以更改此行为。 Check documentation section on cascades in Hibernate here: custom cascades .
在此处查看有关Hibernate中级联的文档部分: 自定义级联 。 You can change mapping like this:
您可以像这样更改映射:
class User {
static hasMany = [texts: Text]
static mapping = {
texts cascade: "save-update"
}
}
Finally I found the right hint . 终于我找到了正确的提示 。 It seems that this is a casting problem.
看来这是一个强制性问题。 see:
看到:
Groovy Primitive Type Cast Gotcha When Upgrading to Grails 2.0 升级到Grails 2.0时的Groovy基本类型强制转换
So i changed the primitive "long" property of the Text domain class to its "boxed" version "Long": 因此,我将Text域类的原始“ long”属性更改为其“装箱”版本“ Long”:
package net.peddn
class Text {
Long creator
String title
static constraints = {
creator( nullable: true )
}
}
and now the property can be set to null! 现在该属性可以设置为null!
Thank you Tomasz for your help! 谢谢Tomasz的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.