Hibernate支持Postgresql UUID?

编程入门 行业动态 更新时间:2024-10-22 13:43:22
本文介绍了Hibernate支持Postgresql UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法让Hibernate使用PostgreSQL的java.util.UUID。

这里是使用javax.persistence。*注释的映射:

private UUID itemUuid; @Column(name =item_uuid,columnDefinition =uuid NOT NULL) public UUID getItemUuid(){ return itemUuid; } public void setItemUuid(UUID itemUuid){ this.itemUuid = itemUuid; }

当持久化一个瞬态对象时,我得到一个SQLGrammarException:

列item_uuid的类型为uuid,但表达式的字符类型为bytea 149

解决方案

这可以通过将以下注释添加到UUID:

import org.hibernate.annotations.Type; ... @Type(type =pg-uuid) private java.util.UUID itemUuid;

至于Hibernate为什么不把它设为默认设置,我无法告诉你。 ..

更新:使用createNativeQuery方法来打开具有UUID字段的对象似乎仍然存在问题。幸运的是,到目前为止,createQuery方法对我来说工作得很好。

I can't get Hibernate working with java.util.UUID for PostgreSQL.

Here is the mapping using javax.persistence.* annotations:

private UUID itemUuid; @Column(name="item_uuid",columnDefinition="uuid NOT NULL") public UUID getItemUuid() { return itemUuid; } public void setItemUuid(UUID itemUuid) { this.itemUuid = itemUuid; }

When persisting a transient object I get a SQLGrammarException:

column "item_uuid" is of type uuid but expression is of type bytea at character 149

PostgreSQL version is 8.4.4 JDBC driver - 8.4.4-702 (also tried 9.0 - same thing) Hibernate version is 3.6, main configuration properties:

<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://192.168.1.1/db_test</property>

解决方案

This can be solved by adding the following annotation to the UUID:

import org.hibernate.annotations.Type; ... @Type(type="pg-uuid") private java.util.UUID itemUuid;

As to why Hibernate doesn't just make this the default setting, I couldn't tell you...

UPDATE: There still seem to be issues using the createNativeQuery method to open objects that have UUID fields. Fortunately, the createQuery method so far has worked fine for me.

更多推荐

Hibernate支持Postgresql UUID?

本文发布于:2023-10-16 05:42:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Hibernate   Postgresql   UUID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!