【发布时间】:2021-09-08 16:48:01
【问题描述】:
有一个过滤器端点可以查询多个属性。 (使用的是Spring Boot 2.4.5) 这是实体:
@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 是一个自定义的 Hibernate UserType。在postgres中,该列的定义是varchar[]。
这是存储库:
@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);
}
但是,我得到一个异常,原因如下:
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
我尝试使用原生查询:
@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);
但不幸的是,我得到以下原因:
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
(位置 116 是 otherNames 查询开始的位置)。
我不想将otherNames 转换为varchar,因为我提供了List,因此这也不会导致有效的查询。
有谁知道如何让这个查询在本机查询(postgres 10)或 JPQL 中工作?
【问题讨论】:
-
您能否再次检查删除查询中的
:otherNames is not null部分?我认为这是它正在打破的地方 -
不幸的是,这不起作用,得到了同样的错误
标签: java postgresql spring-boot hibernate jpa