Wordpress Grails只有一个定义null

Wordpress Grails只有一个定义null,wordpress,hibernate,grails,groovy,Wordpress,Hibernate,Grails,Groovy,我有一个Grails项目。它的域对象是从现有的WordPress数据库反向工程的 我有一个名为WpPosts的类,它看起来是这样的: Date postDate //[...] lots of stuff here which is not important Long commentCount static mapping = { version false //more blah blah } static hasOne = [postAuthor: WpUsers, po

我有一个Grails项目。它的域对象是从现有的WordPress数据库反向工程的

我有一个名为WpPosts的类,它看起来是这样的:

Date postDate
//[...] lots of stuff here which is not important
Long commentCount

static mapping = {
    version false
   //more blah blah
}

static hasOne = [postAuthor: WpUsers, postParent: WpPosts]
static hasMany = [childPosts: WpPosts]
static constraints = {
        postParent nullable: true
//even more blah blah blah
    }
因此,一个帖子有可能像孩子一样拥有帖子。但一个帖子不一定要有家长。 在数据库中,如果未定义父id,则父id为0。 如果我现在尝试获取帖子,grails会尝试获取id为0的父对象。这是不存在的。所以我得到了一个

Method threw 'org.hibernate.ObjectNotFoundException' exception.
我当然可以将父项定义为长值。但我会失去很多安慰。所以这不是我想要的解决方案

提前感谢您的回答


编辑:我现在的问题是我是否做错了什么。或者我可以定义0是我的空对象吗?

处理此问题的一种方法是拦截getPostParent方法调用并实现自己的加载逻辑。例如:

class WpPosts {
  Date postDate
  //[...] lots of stuff here which is not important
  Long commentCount

  static mapping = {
    version false
   //more blah blah
  }

  static hasOne = [postAuthor: WpUsers, postParent: WpPosts]
  static hasMany = [childPosts: WpPosts]
  static constraints = {
    postParent nullable: true
    //even more blah blah blah
  }

  WpPosts getPostParent(Long id) {
    if (id == 0) return null
    return WpPosts.get(id)
  }
}

我没有尝试过这个,但它可能会帮助您克服您的问题。

您尝试过使用
wppposts postParent
作为属性而不是
hasOne
关联吗?今晚我会尝试