3

I have an entity that has a UUID column (It's not primary key). I'm using Postgres and hibernate. The type I have for this column is https://www.postgresql.org/docs/9.1/static/datatype-uuid.html .

If I do

@Type(type = "pg-uuid")
@Column(name = "uuid", unique = true, updatable = false)
private UUID myUuid;

Everything works just fine. But, I don't want this column to be a UUID and I want it to be a Java String just for the sake of it. When I change to

@Type(type = "pg-uuid")
@Column(name = "uuid", unique = true, updatable = false)
private String myUuid;

I get

ClassCastException: java.lang.String cannot be cast to java.util.UUID

When I do

@Column(name = "uuid", unique = true, updatable = false)
private String myUuid;

I get

PSQLException: ERROR: column "uuid" is of type uuid 
but expression is of type character varying

When I do

@Convert(converter = UUIDAttributeConverter.class)
@Column(name = "uuid", unique = true, updatable = false, columnDefinition = "uuid")
private String uuid;

where UUIDAttributeConverter is:

@Converter
public class UUIDAttributeConverter implements  
AttributeConverter<String, PGobject> {
  @Override
  public PGobject convertToDatabaseColumn(String uuid) {
    PGobject toInsertUUID = new PGobject();
    toInsertUUID.setType("uuid");
    try {
        toInsertUUID.setValue(UUID.fromString(uuid).toString());
    } catch (SQLException e) {
        throw new IllegalArgumentException();
    }

    return toInsertUUID;
  }

  @Override
  public String convertToEntityAttribute(PGobject dbData) {
    return dbData.toString();
  }
}

I get

PSQLException: ERROR: column "uuid" is of type uuid but expression is of type bytea

Hint: You will need to rewrite or cast the expression.

When I do the converter again but this time where converter is

@Converter
public class UUIDAttributeConverter implements AttributeConverter<UUID, 
String> {
  @Override
  public UUID convertToEntityAttribute(String s) {
    return UUID.fromString(s);
  }

  @Override
  public String convertToDatabaseColumn(UUID id) {
    return id.toString();
  }
}

I get

PSQLException: ERROR: column "uuid" is of type uuid but expression is 
of type bytea
Hint: You will need to rewrite or cast the expression.

What am I missing here? I would greatly appreciate help.

Fand
  • 88
  • 1
  • 7
  • https://stackoverflow.com/questions/29118210/understanding-hibernate-type-annotation So I would say `@Type` and `@Convert` –  Jul 29 '17 at 13:47
  • related: https://stackoverflow.com/questions/4495233/postgresql-uuid-supported-by-hibernate –  Jul 29 '17 at 13:48
  • @RC. Type can convert cannot be used together. We will have AnnotationException: AttributeConverter and explicit Type cannot be applied to same attribute. I don't have the same issue as https://stackoverflow.com/questions/4495233/postgresql-uuid-supported-by-hibernate . For me if I try to use Java UUID, it's all ok. But not the same if my uuid is a java string. Cheers – Fand Jul 29 '17 at 14:06

0 Answers0