Note that there are some explanatory texts on larger screens.

plurals
  1. POPersisting UUID in PostgreSQL using JPA
    primarykey
    data
    text
    <p>I'm trying to persist an entity in PostgreSQL that uses UUID as primary key. I've tried persisting it as a plain UUID:</p> <pre><code>@Id @Column(name = "customer_id") private UUID id; </code></pre> <p>With the above, I get this error: </p> <pre><code>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 </code></pre> <p>I also tried persisting the UUID as byte[] to no avail:</p> <pre><code>@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)); } </code></pre> <p>If I remove @Lob, the error I get is the same as the one posted above. But with @Lob applied, the error changes slightly to:</p> <pre><code>ERROR: column "customer_id" is of type uuid but expression is of type bigint Hint: You will need to rewrite or cast the expression. Position: 137 </code></pre> <p>I'm feeling extremely bad being unable to do something as simple as this! </p> <p>I'm using Hibernate 4.1.3.Final with PostgreSQL 9.1. </p> <p>I've seen numerous questions on SO more or less with the same issue but they are all old and none seems to have a straight forward answer. </p> <p>I'd like to achieve this in a standard way without resorting to ugly hacks. But if this can be achieved only though (ugly) hacks, then may be that's what I'll do. However, I don't want to store the UUID as a varchar in the database as that's not good for performance. Also, I'd want to not introduce a Hibernate dependency in my code if possible.</p> <p>Any help would be greatly appreciated.</p> <p><em>UPDATE 1</em> (2012-07-03 12:15 pm)</p> <p>Well, well, well... It's kinda interesting that I tested the exact same code (plain UUID, no conversion -- the first version of the code posted above) with SQL server 2008 R2 using the JTDS driver (v1.2.5) and, guess what, it worked as a charm (of course I had to change connection-related info in persistence.xml). </p> <p>Now, is it a PostgreSQL-specific issue or what?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload