Hibernate支持Postgresql UUID? [英] Postgresql UUID supported by Hibernate?

查看:112
本文介绍了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 
$ b $ PostgreSQL版本是8.4.4

JDBC驱动程序 - 8.4.4-702(也试过9.0 - 同样的东西)

Hibernate的版本是3.6,主要配置属性:










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


解决方案

这可以通过将以下注释添加到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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆