【问题标题】:GORM / Hibernate Not Creating Correct INSERT StatementGORM / Hibernate 没有创建正确的 INSERT 语句
【发布时间】:2015-07-07 18:47:01
【问题描述】:

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/
  • 使项目可以为空:true
  • 转储数据库并重新创建它
  • 已验证我在 save() 之前获得了与域关联的适当项目
  • 我也试过 project.addToProjectDocuments() 但这给了我一个无效的参数错误

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

更新:我将域的名称重构为 ProjectDocumentFix 以查看它是否可以正常工作,并且看,它可以正常工作。所以 project_document / ProjectDocument 有什么地方弄乱了,我不知道它可能是什么。

更奇怪的是,这不仅发生在我的本地机器上,也发生在我们的暂存环境中。

【问题讨论】:

  • Auditablesrc/groovy 还是grails-app/domain 中?
  • 它在 src/groovy 中。
  • 你在保存Project和ProjectDocument保存是级联的吗?您还可以添加项目域吗?是否有人(最有可能是 DBA)修改了 Project 的序列?是否修改了 Project 的生成器策略?您还可以在 Project 被持久化和/或获取的地方添加 SQL 查询吗?

标签: mysql hibernate grails grails-orm


【解决方案1】:

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

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

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

static hasMany = [
  projectDocuments: ProjectDocument  
]

删除 getter 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多