grails:如何使用复合外键映射一对多关系

grails: How to map one to many relationship with composite ForeignKey

我有一个预先存在的数据库,其中包含两个表 FileContainer 和 FileObjects。 FileContainer 具有 clientId 和 fileContainerId 的复合主键。 FileContainerId 每个客户端都是唯一的。 FileObjects 还有一个复合主键 clientId(FileContainerClientId) 和 fileObjectId。 FileContainer 和 FileObjects 具有一对多的关系。外键关系有多个列(clientId 和 fileContainerId)。所以我的域 classes 如下所示。

class FileContainer implements Serializable {

  BigDecimal clientId
  BigDecimal fileContainerId
  ...
  ...
  static hasMany = [fileObjects: FileObject]

  static mapping = {
    table 't_container'
    id composite : ["clientId", "fileContainerId"]
  }
}

class FileObject implements Serializable {

  BigDecimal FileContainerClientId
  BigDecimal fileObjectId
  FileContainer fileContainerId
  ...
  ...
  static belongsTo = [FileContainer]

  static mapping = {
    table 't_file_object'
    id composite : ["FileContainerClientId", "fileContainerId"]
    column fileContainerId: 'custom_name_container_id'
    column FileContainerClientId: 'client_id'
  }
}

但不幸的是,上述域 classes 验证失败,并给出以下带有文本 org.hibernate.MappingException 的异常:'Repeated column in mapping for entity: FileObject column: file_container_client_id (should be mapped with insert="false" update="false")'。

我尝试使用由列(client_id 和 file_container_id)组成的自定义 UserType。但是出现了各种不同的错误。

在这种情况下应该如何创建域 classes?

此外,如果有人可以给出使用 org.hibernate.usertype.UserType 的示例,那将会很有帮助。

============================================= ================== 还在 FileObject class.

中尝试了以下内容
class FileObject implements Serializable {

  BigDecimal clientId
  BigDecimal fileObjectId
  BigDecimal fileContainerId
  ...
  ...
  static belongsTo = [fileContainer: FileContainer]

  static mapping = {
    table 'file_object'
    id composite : ["FileContainerClientId", "fileContainerId"]
    column fileContainerId: 'custom_name_container_id'
    column clientId: 'client_id'
    fileContainer column: 'client_id'
    fileContainer column: 'file_container_id'
  }
}

但是得到了丢失的 file_container_client_id 列异常。

============================================= ============================ 由于 Object class 中的 FileContainerClientId 是 Container class 中 clientId 的外键,因此不应直接插入或更新它,它应始终来自 FileContainer class。按照上述逻辑,我尝试添加以下映射

static mapping = { 
  FileContainerClientId insertable: false 
  FileContainerClientId updateable: false 
}

但编译失败,因为 属性 名称以大写字母开头。如果我将 属性 名称更改为 fileContainerClientId 然后它编译但无法将该列映射到 foreignKey 并在验证期间抛出丢失的 file_container_client_id 列异常

下面的代码可以用来映射这种情况。

class FileContainer implements Serializable {

  BigDecimal clientId
  BigDecimal fileContainerId
  ...
  ...
  static hasMany = [fileObjects: FileObject]

  static mapping = {
    table 't_container'
    id composite : ["clientId", "fileContainerId"]
  }
}

class FileObject implements Serializable {

  BigDecimal clientId
  BigDecimal fileObjectId
  BigDecimal fileContainerId
  FileContainer fileContainer
  ...
  ...
  static belongsTo = [FileContainer]

  static mapping = {
    table 't_file_object'
    id composite : ["clientId", "fileObjectId"]
    columns {
        fileContainer {
            column name:'client_id'
            column name:'file_container_id'
        }
    }
    fileContainer insertable:false, updateable:false

  }
}