0
votes

I'm getting exception:

org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

I have JPA entity

@Entity(name = "products")
class JpaProductEntity {

    @Id
    private ProductId id;

    private String payload;

    private Integer version;

    public JpaProductEntity() {
    }

    public void setId(ProductId id) {
        this.id = id;
    }

    public String getPayload() {
        return payload;
    }

    public void setPayload(String payload) {
        this.payload = payload;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}

Id class

 public class ProductId  implements Serializable {

    private static final long serialVersionUID = -6110163899364126142L;

    private String identifier;

    public ProductId(String identifier) {
        this.identifier = identifier;
    }

    public static ProductId from(String identifier) {
        Assert.notNull(identifier, "Identifier cannot be null");
        return new ProductId(identifier);
    }

    public static ProductId generate() {
        return new ProductId(Id.generateIdentifier());
    }
}

Also, converter:

@Converter(autoApply = true)
public class ProductIdPostgresUuidConverter implements AttributeConverter<ProductId, UUID> {

    @Override
    public UUID convertToDatabaseColumn(ProductId attribute) {
        return UUID.fromString(attribute.toString());
    }

    @Override
    public ProductId convertToEntityAttribute(UUID dbData) {
        return ProductId.from(dbData.toString());
    }
}

It seems, converter does not work at all (I've put break points in converter methods, but nothing happens, debugger does not enter there).

1
Is your JPA provider supporting JPA 2.1? Does the converter work for you on non-PK fields? Which JPA provider? Have you included the converter explicitly in persistence.xml? - Neil Stockton
You could try annotating the ProductId class with @Embeddable and the id in JpaProductEntity as @Embedded. No conversion would be required then. Of course ProductId will need a getter and setter for identifier and a no-argument constructor. - coladict

1 Answers

0
votes

Try this:

@Entity(name = "products")
class JpaProductEntity {

  @Type(type = "pg-uuid") // tells what type will be internally used by DB
  // Definitely need to instruct JPA to use your converter
  @Convert(converter = ProductIdPostgresUuidConverter.class)
  @Id
  private ProductId id;

}

Hint: you may have multiple converters for the same type, to be used at your discretion. You do need to tell JPA explicitly which one to use.