我无法让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 149PostgreSQL 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?
发布评论