【问题标题】:Using an uuid or guid as id in grails/hibernate在 grails/hibernate 中使用 uuid 或 guid 作为 id
【发布时间】:2014-05-19 11:08:48
【问题描述】:

我需要将 GUID/UUID 作为我的行的 id 列。

这是为了能够在线和离线创建条目,当然在合并时不会在PK上出现这些冲突。我知道我可以缓解这种情况,但我想保持简单,并且已经有遗留应用程序使用 uuid/guids 来定义关系)。以后还需要双向同步数据。重写现有应用程序不是一种选择。

当我尝试将 GUID 或 UUID 与 grails 一起使用时,我收到错误 500。(在 h2 上使用 GUID 会导致另一个错误 - 说明 DB 不支持 GUID,正如预期的那样)。

当我尝试保存“WithUUID”时出现此错误:

URI    /gtestUUID/withUUID/save
Class    java.lang.IllegalArgumentException
Message    argument type mismatch

整个错误 500: http://imgur.com/m2Udbm6.png

我尝试过使用 mariadb 5.5 和 1.1.7 驱动程序,这会导致同样的问题。

Grails 2.3.8。视窗 8.1 (x64)。网豆 7.4

全部默认。

示例类:

WithUUID.groovy:

package gtestuuid

class WithUUID {
    String name
    static constraints = {
    }
    static mapping = {
        id generator: 'uuid'
    }
}

WithUUIDController.groovy:

package gtestuuid

class WithUUIDController {
    def scaffold = WithUUID
}

任何帮助将不胜感激。

Link to relevant grails documentation

(由于我的代表率低,我无法发布更多文档/帖子的链接)

【问题讨论】:

  • 我认为您需要将 id 设置为 String 或 UUID(或任何您需要的)
  • 谢谢!我从来没有考虑过这个选项。请作为答案提交,我会标记为正确
  • 完成! (还有另一种可能)

标签: hibernate grails groovy hibernate-mapping


【解决方案1】:

或者您可以在类中指定 UUID id 字段,而无需像本例中那样进行任何初始化和设置映射

class Buyer {

    UUID id
    String name
    String phone
    String email

    static constraints = {
    }

    static mapping = {
        id generator : 'uuid2', type: 'pg-uuid' // pg-uuid because I use postgresql
    }

}

你可以在 Config.groovy 中取出映射

grails.gorm.default.mapping = {
    id generator : 'uuid2', type: 'pg-uuid'
}

您的课程将仅包含 UUID id 字段,没有任何冗余代码

【讨论】:

    【解决方案2】:

    您需要将 id 设置为 String 或 UUID(或任何您需要的)。

    下面,我的类 User 的一个例子还有另一种可能性:

    import java.util.UUID
    
    class User {
    
        String id = UUID.randomUUID().toString()
    
        static mapping = {
            id generator:'assigned'
        }
    }
    

    【讨论】:

    • 我推荐使用这个方法而不是另一个,另一个(生成器:'uuid2')导致很多问题,我无法通过id检索任何记录
    【解决方案3】:

    这是我在 Grails 3 域类中所做的...

    class MyClass {    
        static constraints = {
        }
    
        UUID id
        static mapping = {
            id generator: "UuidGenerator", length: 36, type: "uuid-char"
    
       }
    
    
    }
    

    这就是我在 UuidGenerator.groovy 中的内容

    import org.hibernate.id.IdentifierGenerator;
    import  org.hibernate.engine.spi.SessionImplementor
    
    import java.security.SecureRandom;
    
    /**
     * 
     */
    class UuidGenerator implements IdentifierGenerator {
        Serializable generate(SessionImplementor session, Object object) {
    
            UUID.randomUUID()
        }
    }
    

    对于 grails 2.x.x,我的 UuidGenerator.groovy 看起来像这样...

    import org.grails.datastore.mapping.core.SessionImplementor
    import org.hibernate.id.IdentifierGenerator;
    import org.hibernate.engine.SessionImplementor;
    
    
    /**
     * 
     */
    class UuidGenerator implements IdentifierGenerator {
        Serializable generate(SessionImplementor session, Object object) {
    
            return UUID.randomUUID().toString()
        }
    }
    

    我的域类看起来像这样......

    class MyClass {    
        static constraints = {
    
        }
    
         String id 
        static mapping = {
            id generator: "com.novadge.UuidGenerator", length:36
        }    
    }
    

    【讨论】:

      【解决方案4】:

      如果您将 id 指定为 String 和生成器类型“uuid”,您可以轻松地在 Grails 3 中为您的域获得 uuid 行为。

      class State  {
          String id
          String state 
      
          static mapping = {
              id generator: 'uuid'
          }
      

      【讨论】:

        猜你喜欢
        • 2012-03-02
        • 1970-01-01
        • 2011-07-04
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多