Home > Enterprise >  Operator does not exist: integer ~~ integer
Operator does not exist: integer ~~ integer

Time:08-13

When sending postman get request for findbyid it gives a 500 internal server error and the following error in the terminal: org.postgresql.util.PSQLException: ERROR: operator does not exist: integer ~~ integer Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

My User repository interface

public interface UserEntityRepository extends JpaRepository<UserEntity,Integer> {

    UserEntity findByName(String name);

    @Query(value = "select * from user_table where cast(id as int) like :id",nativeQuery = true)
    UserEntity findByUserById(Integer id);

My base Entity for creating ids:

public class BaseEntity {
    @Getter
    @Setter
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
}

My psql code:

id serial not null
        constraint user_table_pk
            primary key,

CodePudding user response:

The like operator is only defined for character datatypes (you cannot write a query some_int_column like '%1%').

The query for UserEntityRepository.findByUserById() must be written as

@Query(value = "select * from user_table where id = :id",nativeQuery = true)
  • Related