Grails中可空属性的TransientObjectException

时间:2013-09-13 00:39:31

标签: hibernate grails gorm nullable

我有以下实体

class Thesis extends Article {
   String description
   Topic topic
   User assignee
   User supervisor
   ...
   static constraints = {
       supervisor nullable: true
   ...
      supervisor validator: {supervisor ->
         if (supervisor?.id != null && !User.get(supervisor.id)) {
            'not.found'
         }
      }
   }
}

创建并保存没有主管的新实例。

但是当我尝试从现有的Thesis实例中删除(设置为null)supervisor时,我最终得到了TransientObjectException。我可能遗漏了一些关于数据绑定的事情,因为如果我没有将表单数据绑定到thesisInstance并且只是将“supervisor”属性设置为null,它将被保存而没有任何问题。

def thesisInstance = Thesis.get(id)
thesisInstance.properties = params.thesis
if (thesisInstance.supervisorId == null){
   thesisInstance.supervisor = null
}
thesisInstance.save()

编辑:params.thesis地图看起来像这样(免责声明:这些是虚拟数据)

thesis.tags.title: ruby
thesis.version: 7
thesis.topic.id: 13
thesis.supervisor.id: 
thesis.status: FINISHED
thesis.id: 18
thesis.title: Implementation of Ruby Killing Machine
thesis.type: BACHELOR
thesis.assignee.fullName: Somebody
thesis.university.id: 1
thesis.grade: A
thesis.supervisor.fullName: 
thesis.thesisAbstract: Implementation of Ruby Killing Machine that will kill people that are filled in an HTML form.
thesis.description: Implementation of Ruby Killing Machine that will kill people that are filled in an HTML form.
thesis.assignee.id: 4

正如您所看到的,没有填充thesis.supervisor.id,因此,即使用ID,也可以将thesisInstance.supervisor设置为新的User实例。因此需要“thesisInstance.supervisor = null”

我在[1]上发现完全相同的问题,但没有答案。

[1] http://grails.1312388.n4.nabble.com/Data-Binding-and-Null-ifying-problems-in-G1-2-0-td1339929.html

0 个答案:

没有答案