问题描述
我无法让 Hibernate 使用 java.util.UUID for PostgreSQL.
I can't get Hibernate working with java.util.UUID for PostgreSQL.
这是使用 javax.persistence.* 注释的映射:
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;
}
当持久化一个临时对象时,我得到一个 SQLGrammarException:
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 版本为 8.4.4
JDBC 驱动程序 - 8.4.4-702(也尝试过 9.0 - 同样的事情)
Hibernate版本为3.6,主要配置属性:
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>
推荐答案
这可以通过在 UUID 中添加以下注解来解决:
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;
至于为什么 Hibernate 不只是将其设为默认设置,我无法告诉您...
As to why Hibernate doesn't just make this the default setting, I couldn't tell you...
更新:使用 createNativeQuery 方法打开具有 UUID 字段的对象似乎仍然存在问题.幸运的是,到目前为止,createQuery 方法对我来说效果很好.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!