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.