GORM / Hibernate没有创建正确的INSERT语句

时间:2015-04-28 18:49:42

标签: mysql hibernate grails gorm

Grails 2.2.4

我不知道为什么,因为这是有效的,然后就是突然停止了。我有以下域名:

class ProjectDocument extends Auditable {
  Integer externalId
  String name
  Boolean completed = false
  Boolean purchased = false
  Boolean modifiedAfterPublish = true
  Integer revisionNumber
  String registrationNumber
  User documenter
  User fieldTechnician
  User registrant
  User enforcementAgent
  Date enforcementDateChecked
  PublishedDocument document
  Employer enforcementOrg
  ProjectDocumentEntry currentEntryCursor

  static belongsTo = [
    project : Project
  ]

  static hasMany = [
      projectDocumentEntries: ProjectDocumentEntry
  ]

  static constraints = {
    name nullable: false, blank: false, maxSize: 50
    purchased nullable: false
    externalId nullable: false
    completed nullable: false
    revisionNumber nullable: false
    registrationNumber nullable: true, blank: false, maxSize: 50, unique: true
    enforcementDateChecked nullable: true
    project nullable: true
    document nullable: false
    enforcementOrg nullable: true
    documenter nullable: true
    fieldTechnician nullable: true
    registrant nullable: true
    enforcementAgent nullable: true
    modifiedAfterPublish nullable: true
    currentEntryCursor nullable: true
  }
}

这是可审核域名:

abstract class Auditable implements Serializable {

  Date dateCreated
  Date lastUpdated
  User createdBy
  User modifiedBy

  static constraints = {
    dateCreated nullable: true
    lastUpdated nullable: true
    createdBy nullable: false
    modifiedBy nullable: false
  }
}

当我尝试将数据插入此表时,我得到以下SQL:

insert 
    into
        project_document
        (version, completed, created_by_id, current_entry_cursor_id, date_created, document_id, documenter_id, enforcement_agent_id, enforcement_date_checked, enforcement_org_id, external_id, field_technician_id, last_updated, modified_after_publish, modified_by_id, name, purchased, registrant_id, registration_number, revision_number) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

请注意缺少project_id列。因此,我收到了这个错误:

Field 'project_id' doesn't have a default value

我已采取措施解决问题但未解决问题:

  • grails clean
  • 删除.grails / 2.2.4 / projects /
  • 制作项目可空:真实
  • 转储数据库并重新创建
  • 已验证我在save()
  • 之前获得了与该域关联的相应项目
  • 我也尝试过project.addToProjectDocuments(),但这会给我一个无效的参数错误

我真的不知道为什么这似乎突然不起作用。

更新:我将域的名称重构为ProjectDocumentFix以查看它是否可行,并且低并且看起来有效。所以有一些东西与project_document / ProjectDocument混淆了,我不知道它可能是什么。

为了使事情变得更加奇怪,这不仅发生在我的本地机器上,也发生在我们的临时环境中。

1 个答案:

答案 0 :(得分:0)

好的,我不知道为什么会出现这个问题,但在我们的Project域中,我注意到以下内容:

List<ProjectDocument> getProjectDocuments() {
  ProjectDocument.findAllByProject(this)
}

这没有任何意义,因为我们也有这个:

static hasMany = [
  projectDocuments: ProjectDocument  
]

删除getter解决了这个问题。