我试图在PostgreSQL中持久化一个使用UUID作为主键的实体.我已经尝试将其作为简单的UUID持久化:
@Id
@Column(name = "customer_id")
private UUID id;
Run Code Online (Sandbox Code Playgroud)
有了上面的内容,我收到了这个错误:
ERROR: column "customer_id" is of type uuid but expression is of type bytea
Hint: You will need to rewrite or cast the expression.
Position: 137
Run Code Online (Sandbox Code Playgroud)
我也尝试将UUID作为byte []保持无效:
@Transient
private UUID id;
@Id
@Column(name = "customer_id")
@Access(AccessType.PROPERTY)
@Lob
protected byte[] getRowId() {
return id.toString().getBytes();
}
protected void setRowId(byte[] rowId) {
id = UUID.fromString(new String(rowId));
}
Run Code Online (Sandbox Code Playgroud)
如果我删除@Lob,我得到的错误与上面发布的错误相同.但是应用@Lob后,错误会略有变化:
ERROR: column "customer_id" is of type uuid but expression is of type bigint
Hint: You will …Run Code Online (Sandbox Code Playgroud) 我不能让Hibernate使用java.util.UUID for PostgreSQL.
这是使用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;
}
Run Code Online (Sandbox Code Playgroud)
当持久化对象时,我得到一个SQLGrammarException:
column "item_uuid" is of type uuid but expression is of type bytea at character 149
Run Code Online (Sandbox Code Playgroud)
PostgreSQL版本是8.4.4
JDBC驱动程序--8.4.4-702(也试过9.0 - 同样的事情)
Hibernate版本是3.6,主要配置属性:
<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>
Run Code Online (Sandbox Code Playgroud)