【问题标题】:How to change Grails embedded column mappings如何更改 Grails 嵌入的列映射
【发布时间】:2012-03-06 21:57:37
【问题描述】:

我正在考虑将我的应用程序的持久性映射从休眠 hbm 文件移动到 grails 域对象。该模式不符合 Grails 的许多列命名约定,包括组合列名。我想做的是:

class Foo{
   Bar bar
   static embedded = ['bar']
   static mapping = {
         bar.baz column:'baz'
         bar.quz column:'qux'
   }
}

class Bar{
  String baz, qux
}

这个问题有jira。不幸的是,它已经开放了将近两年,没有任何变化。是否有解决方法来解决数据库中缺少更改列的问题?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    不要使用嵌入变量,而是为您的 Bar 类创建一个自定义休眠用户类型。然后,您可以将该自定义类型映射到您想要的任何列名称:

    static mapping = {
        bar type: BarUserType, { 
            column name: "bar"
            column name: "quz"
        }
    }
    

    【讨论】:

      【解决方案2】:

      我相信目前唯一的方法是(grails 2.1)将映射放在 Bar 中,

      class Bar {
          String bar, quz
      
          static mapping = {
              baz column: "baz"
              quz column: "quz"
          }
      }
      

      【讨论】:

        【解决方案3】:

        我发现的一种解决方法是使用@grails.util.Mixin 而不是嵌入:

        @grails.util.Mixin(Bar)
        class Foo{
           static mapping = {
                 baz column:'bazz'
                 quz column:'quxx'
           }
        }
        
        class Bar{
          String baz, qux
        }
        

        【讨论】:

          猜你喜欢
          • 2012-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-20
          • 1970-01-01
          • 1970-01-01
          • 2021-06-25
          • 2012-11-11
          相关资源
          最近更新 更多