Java 错误:运算符不存在:spring jpa查询和postgres中的boolean=bytea
当我在postgres中直接执行below查询时,它工作正常,但当我使用JPA运行时,就会出现below错误 查询示例:Java 错误:运算符不存在:spring jpa查询和postgres中的boolean=bytea,java,sql,postgresql,spring-data-jpa,Java,Sql,Postgresql,Spring Data Jpa,当我在postgres中直接执行below查询时,它工作正常,但当我使用JPA运行时,就会出现below错误 查询示例: select * from products where clientId = :clientId and ((:status is null and status in (true, false)) or status = :status) and ((:anotherStatus is null an
select
*
from
products
where
clientId = :clientId
and ((:status is null
and status in (true,
false))
or status = :status)
and ((:anotherStatus is null
and anotherStatus in (true,
false))
or anotherStatus = :anotherStatus);
@Query(value="select * from products where clientId = :clientId and ((:status is null and status in (true, false)) or status = :status) and ((:anotherStatus is null and anotherStatus in (true, false)) or anotherStatus = :anotherStatus)",nativeQuery=true)
List<Products> fetchProducts(@Param("clientId") Long clientId, @Param("status") Boolean status, @Param("anotherStatus") Boolean anotherStatus);
错误:
select
*
from
products
where
clientId = :clientId
and ((:status is null
and status in (true,
false))
or status = :status)
and ((:anotherStatus is null
and anotherStatus in (true,
false))
or anotherStatus = :anotherStatus);
@Query(value="select * from products where clientId = :clientId and ((:status is null and status in (true, false)) or status = :status) and ((:anotherStatus is null and anotherStatus in (true, false)) or anotherStatus = :anotherStatus)",nativeQuery=true)
List<Products> fetchProducts(@Param("clientId") Long clientId, @Param("status") Boolean status, @Param("anotherStatus") Boolean anotherStatus);
错误org.hibernate.engine.jdbc.spi.SqlExceptionHelper-错误:
运算符不存在:boolean=bytea提示:没有匹配的运算符
给定的名称和参数类型。您可能需要添加显式类型
演员
在JPA中配置如下:
select
*
from
products
where
clientId = :clientId
and ((:status is null
and status in (true,
false))
or status = :status)
and ((:anotherStatus is null
and anotherStatus in (true,
false))
or anotherStatus = :anotherStatus);
@Query(value="select * from products where clientId = :clientId and ((:status is null and status in (true, false)) or status = :status) and ((:anotherStatus is null and anotherStatus in (true, false)) or anotherStatus = :anotherStatus)",nativeQuery=true)
List<Products> fetchProducts(@Param("clientId") Long clientId, @Param("status") Boolean status, @Param("anotherStatus") Boolean anotherStatus);
@Query(value=“select*from products,其中clientId=:clientId和(:status为null,status为(true,false))或status=:status)和(:anotherStatus为null,anotherStatus为(true,false))或anotherStatus=:anotherStatus)”,nativeQuery=true)
列出fetchProducts(@Param(“clientId”)Long clientId、@Param(“status”)Boolean status、@Param(“anotherStatus”)Boolean anotherStatus);
基本上,我试图实现的是,当用户将param作为
true
发送时,使用true
(活动)或作为false
(活动)过滤产品,当用户不发送任何内容时(即null
),然后获取所有产品(true
和false
)请记住,null=null
、true=null
和false=null
都计算为null
,在where
-子句中被视为false
。如果你还需要什么,请重新表述你的问题。
让我们看看(:status为null,status为(true,false))或status=:status
的真值表
|-------------|-------------|-------------|
| :status | status | result |
|-------------|-------------|-------------|
| null | null | false |
| null | true | true |
| null | false | true |
| true | null | false |
| true | true | true |
| true | false | false |
| false | null | false |
| false | true | false |
| false | false | true |
|-------------|-------------|-------------|
换句话说,当status
为null
时,您需要一个始终返回false
的子句。当:status
为null
时,这将使为true
,如果:status
和status
具有相同的值,则为true
状态不为null且(:status为null或status=:status)
或者
状态不为空且合并(:状态,状态)=状态
请注意,对于您描述的错误,这仍然失败,因为在Java中传递null
时,Hibernate将:status
绑定为BYTEA
。你可以通过双重施放来解决这个问题
CAST(CAST(:状态为字符变化)为布尔值)
如果这是您想要的,则生成的查询将是:
SELECT *
FROM products
WHERE status IS NOT NULL
AND anotherStatus IS NOT NULL
AND COALESCE(CAST(CAST(:status AS CHARACTER VARYING) AS BOOLEAN), status) = status
AND COALESCE(CAST(CAST(:anotherStatus AS CHARACTER VARYING) AS BOOLEAN), anotherStatus) = anotherStatus
:状态为空
,:另一个状态为空
在我看来不受支持know@Eklavya-在JPA?中不受支持,那么我如何才能真正做到这一点…您能帮助我吗?可能在SQL中不受支持,请使用JPA规范进行搜索