【发布时间】:2021-09-15 05:52:03
【问题描述】:
我正在使用 JPA/Hibernate。所以我想在 JPQL 中做 nullCheck,但是当我这样做时它并不能确定 dataType。
JPQL 查询:
@Query("select a from Attribute a where :attributeId is null OR a.id = :attributeId")
Page<Attribute> findByAttributeId(@Param("attributeId") UUUID attributeId);
例外:
原因:org.postgresql.util.PSQLException: ERROR: could not 确定参数 $1 的数据类型 在 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) 在 org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267) 在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312) 在 org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) 在 org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) 在 org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:153) 在 org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:103) 在 jdk.internal.reflect.GeneratedMethodAccessor582.invoke(未知来源) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 在 org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) 在 com.sun.proxy.$Proxy398.executeQuery(未知来源) 在 org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60) ... 省略了 114 个常用帧
我在互联网上搜索了很多关于此主题的内容,但找不到解决方案。 我不想在服务层处理它。
我试过了:
- PostgreSql CAST 函数
- 检查为字符串
- @Type 注释。
JPQL 中是否有检查 UUID 空值的方法?
【问题讨论】:
-
如果存储库接口,还要添加方法声明。
-
你为什么要这样做?
-
@pirho 对不起,我忘记了。
-
@cjgmj 有时,attributeId 为空。我不想在服务层处理它,因为应用程序中的所有 nullchecks 都是在查询中处理的。出于一致性原因,我想在查询中处理它
-
但是如果你的 id 是 null 你必须从 db 中返回 id 为 null 的对象?
标签: java spring hibernate jpa jpql