Persisting UUID in PostgreSQL using JPA

前端 未结 7 615
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 06:43

I\'m trying to persist an entity in PostgreSQL that uses UUID as primary key. I\'ve tried persisting it as a plain UUID:

@Id
@Column(name = \"customer_id\")
         


        
7条回答
  •  离开以前
    2020-12-01 07:06

    The PostgreSQL JDBC driver has chosen an unfortunately way to represent non-JDBC-standard type codes. They simply map all of them to Types.OTHER. Long story short, you need to enable a special Hibernate type mapping for handling UUID mappings (to columns of the postgres-specific uuid datatype):

    @Id
    @Column(name = "customer_id")
    @org.hibernate.annotations.Type(type="org.hibernate.type.PostgresUUIDType")
    private UUID id;
    

    or more succinctly:

    @Id
    @Column(name = "customer_id")
    @org.hibernate.annotations.Type(type="pg-uuid")
    private UUID id;
    

    Another (better) option is to register org.hibernate.type.PostgresUUIDType as the default Hibernate type mapping for all attributes exposed as java.util.UUID. That is covered in the documentation @ http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch06.html#types-registry

提交回复
热议问题