GORM:对新属性进行null检查会导致QueryException

时间:2018-04-10 09:21:49

标签: gorm isnull executequery

我在以前存在的域类BComponent的GORM映射中添加了一个新属性uuid

static mapping = {
  tablePerHierarchy false
  id column:'bc_id'
  uuid column:'bc_uuid', type:'text'
  name column:'bc_name', type:'text', index:'bc_name_idx'
  normname column:'bc_normname', type:'text', index:'bc_normname_idx'
  version column:'bc_version'
  // ... other properties
}

在执行BootStrap.groovy中处理此新属性的代码时,如...

BComponent.withTransaction() {
  BComponent.executeQuery("select bc.id from BComponent as bc where bc.uuid is null").each { bc_id ->
    BComponent.withNewTransaction {
      BComponent bc = BComponent.get(bc_id)
      bc.generateUuid()
      bc.save()
      bc.discard()
    }
  }
}

...抛出以下异常:

ERROR context.GrailsContextLoaderListener  - Error initializing the application: could not resolve property: uuid of: org.gokb.cred.BComponent [select bc.id from org.gokb.cred.BComponent as bc where bc.uuid is null]; nested exception is org.hibernate.QueryException: could not resolve property: uuid of: org.gokb.cred.BComponent [select bc.id from org.gokb.cred.BComponent as bc where bc.uuid is null]
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: uuid of: org.gokb.cred.BComponent [select bc.id from org.gokb.cred.BComponent as bc where bc.uuid is null]; nested exception is org.hibernate.QueryException: could not resolve property: uuid of: org.gokb.cred.BComponent [select bc.id from org.gokb.cred.BComponent as bc where bc.uuid is null]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:671)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:414)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:416)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:348)
    ...

为什么?我的空检查不正确吗?我从类似的检查中采用了它,工作正常,如:

BComponent.executeQuery("select bc.id from BComponent as bc where bc.normname is null and bc.name is not null") // ...

我忘了什么吗?我已经执行了

$ grails clean

还有什么? THX!

1 个答案:

答案 0 :(得分:0)

我忘了在我的Groovy类代码中添加uuid作为属性。所以:

class BComponent{
  // ...
  String uuid
  // ...
}

解决了这个问题。