The wrong syntax in HQL query. Change the query code to
@Query(value="FROM Chat as c where (first_user = :userId or second_user = :lastId)")
Page<Chat> findNextUserChats(
@Param("userId") long id,
@Param("lastId") long lastId,
@Param("lastUpdatedAt") String lastUpdatedAt,
Pageable pageable);