5

I'm using Hibernate 5.0 + Postgres 9.4

My entities use UUIDs as indentifier.

The project also uses hibernate-spatial.

The id property is annotated simply as

@Id
@GeneratedValue
private UUID id;

After persisting any entity (not only the ones with geometrical data), I get the following error:

column "id" is of type geometry but expression is of type uuid

Looks like there is some conflict in types mapping to me; though I'm not an expert of Hibernate types mapping.

Is there anyone who can help me overcome this issue?

Stefano Cazzola
  • 1,597
  • 1
  • 20
  • 36
  • What is the type of column `ID` in the database? – SubOptimal Nov 19 '16 at 10:00
  • @SubOptimal It is created as `geometry`, because both `UUID` and `Geometry` are mapped to `java.sql.Types.OTHER` – Stefano Cazzola Nov 19 '16 at 12:18
  • You mean you have `CREATE TABLE some_table (id geometry, ...)` and now want to map the `UUID` to it? – SubOptimal Nov 21 '16 at 13:08
  • 1
    @SubOptimal Nope. Schema (in dev mode) was created by Hibernate. As long as I didn't add the spatial integration (hibernate-spatial:5.0.7) the UUID fields were created correctly as `pg-uuid`. After adding spatial stuff, the mapping changed due to the fact that both `java.util.UUID` and `com.vividsolutions.Geometry` are mapped to `java.sql.Types.OTHER`. Now, the problem is how to workaround this mapping conflict. – Stefano Cazzola Nov 23 '16 at 14:09
  • 1
    Maybe this could be the solution https://stackoverflow.com/questions/4495233/postgresql-uuid-supported-by-hibernate#7479035. – SubOptimal Nov 24 '16 at 06:41
  • @SubOptimal yes, this is actually a solution (hence the +1). Though it has a couple of flaws: 1. Entities are Hibernate aware; 2. Entities are database specific. The optimal outcome would be to have all entities reusable with different ORMs and different DBMS. I've been working on that quite some hours now, and the only solution I found is to treat UUIDs as strings (which is not very performant) – Stefano Cazzola Nov 24 '16 at 21:27

1 Answers1

6

Check out this answer and the original discussion thread

Specifying columnDefinition = "uuid" solved exactly the same issue for me.

@Entity
public class MyEntity {
    @Id
    @GeneratedValue
    @Column( columnDefinition = "uuid", updatable = false )
    public UUID getId() {
        return id;
    }
}
Community
  • 1
  • 1
Johnny Doe
  • 3,280
  • 4
  • 29
  • 45