1

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).

2
  • 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? Commented Oct 6, 2016 at 6:40
  • 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. Commented Oct 6, 2016 at 14:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Convert(converter = ProductIdPostgresUuidConverter.class) - I've tried this, but not worked for me.
I don't see where the user says he is using Hibernate, after all that is the only place he could use @Type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.