hibernate - 在 grails/hibernate 中使用 uuid 或 guid 作为 id

标签 hibernate grails groovy hibernate-mapping

我需要将 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 驱动程序,这导致了同样的问题。

chalice 2.3.8。 Windows 8.1 (x64)。网 bean 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

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

最佳答案

或者,您可以在类中指定 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 字段,而没有任何冗余代码

关于hibernate - 在 grails/hibernate 中使用 uuid 或 guid 作为 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736208/

相关文章:

java - 用 BufferedInputStream 包装 PipedInputStream

java - 如何以编程方式生成 hibernate POJO 类?

grails - 测试默认值

grails - Grails-如何将Wiki页面嵌入Grails应用程序

java - 在 groovy 中使用 HTML 格式的 JIRA Rest API 创建发行说明

java - 需要 GPath 查询来查找最大日期 + 字符串字段

Java/ eclipse /hibernate

java - Hibernate 不允许带有 int 字段的嵌入对象为空?

java - 无法转换为内部表示

hibernate - OpenSessionInView 过滤器导致 grails run-app 爆炸?