techkuz
techkuz

Reputation: 3956

PSQLException: ERROR: operator does not exist: character varying = uuid

I have this repository:

@Repository
public interface DomainRepository extends CrudRepository<Domain, String> {
    Domain findByUuid(UUID uuid);
}

I have this entity:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name="a", schema="b")
public class Domain {
    @Id
    private String id;
    private UUID uuid;

}

But when I do Domain d = domainRepository.findByUuid(UUID.randomUUID());

I get PSQLException: ERROR: operator does not exist: character varying = uuid (column type in table is VARCHAR).

How to fix this?

Upvotes: 8

Views: 11817

Answers (2)

ArtemAgaev
ArtemAgaev

Reputation: 108

If specifying type didn't help you also can try smth like this:

public class UUIDConverter implements AttributeConverter<UUID, String> {
    @Override
    public String convertToDatabaseColumn(UUID attribute) {
        return attribute == null ? null : attribute.toString();
    }

    @Override
    public UUID convertToEntityAttribute(String dbData) {
        return dbData == null ? null : UUID.fromString(dbData);
    }
}

and modify you entity field:

@Convert(converter = UUIDConverter.class)
private UUID id;

This solution adds type conversion every time you retrieve entity and also requires you to place @Convert annotation for each uuid field, but as workaround this will do.

Also make sure that column in database has varchar/text type, otherwise it may fail(if you don't want to or can't replace types from uuid to varchar, just create view)

Upvotes: 2

Alien
Alien

Reputation: 15878

Try defining the type for the field as Hibernate unable to understand the type by annotating field with @Type(type="org.hibernate.type.UUIDCharType")

@Type(type="org.hibernate.type.UUIDCharType")
private UUID id;

Upvotes: 3

Related Questions