There's a filter endpoint to query on multiple properties. (Spring Boot 2.4.5 is used) This is the entity:
@Data
@Entity
@Table(name = "organization")
@TypeDefs({@TypeDef(name = "dbArray", typeClass = CustomArrayType.class)})
public class Organization implements Serializable {
@Id
private Long id;
@Column
private String name;
@Column
private String otherName;
@Column(name = "organization_types")
@Type(type = "dbArray")
private List<String> organizationTypes;
}
CustomArrayType is a custom Hibernate UserType. In postgres, the definition of this column is varchar[].
This is the repository:
@Repository
public interface OrganizationRepository extends JpaRepository<Organization, Long> {
@Query(
value =
"select o from Organization o where"
+ " (:organizationType is null OR :organizationType in (o.organizationTypes)) AND"
+ " (:otherNames is null OR o.otherName in (:otherNames))")
List<Organization> findOrganizationByTypeAndOtherNamesIn(
@Param("organizationType") String organizationType,
@Param("otherNames") List<String> otherNames);
}
However, I get an exception with the following cause:
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = character varying[] Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Position: 465
I tried to use a native query:
@Query(
value =
"select * from organization o where"
+ " (:organizationType is null OR :organizationType = ANY(o.organization_types)) AND"
+ " (:otherNames is not null AND o.otherName in (:otherNames))",
nativeQuery = true)
List<Organization> findOrganizationByTypeAndOtherNamesIn(
@Param("organizationType") String organizationType,
@Param("otherNames") List<String> otherNames);
But unfortunately, I get the following cause:
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = bytea
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 116
(position 116 is where otherNames query start).
I don't want to cast the otherNames to a varchar, because I provide a List, thus that will also not result in a working query.
Does anyone know how to get this query working in either a native query (postgres 10) or in JPQL?
:otherNames is not nullpart in your query ? I think this is where it is breaking