如何在jpql中检查uuid空值?

jaql4c8m  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(373)

我正在使用jpa/hibernate。所以我想在jpql中执行nullcheck,但当我这样做时,它并不确定数据类型。
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:错误:无法确定org.postgresql.core.v3.queryexecutorimpl.receiveerrorresponse(queryexecutorimpl.java:2532)处org.postgresql.core.v3.queryexecutorimpl.processresults(queryexecutorimpl.java:2267)处参数$1的数据类型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)位于java.base/java.lang.reflect.method.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.org.hibernate.engine.jdbc.internal.resultsetreturnimpl.extract(resultsetreturnimpl.java:60)处$proxy398.executequery(未知源代码)。。。114省略了公共帧
关于这个主题,我在网上搜索了很多,但找不到答案。我不想在服务层处理它。
我试过:
postgresql转换函数
检查为字符串
@类型注解。
在jpql中是否有检查uuid空值的方法?

2uluyalo

2uluyalo1#

我在JPA3.0+eclipselink+postgresql中也遇到了类似的错误,原因是参数的自动转换。尝试添加一个 stringtype=unspecified 到您的连接url: jdbc:postgresql://SERVER:5432/mydatabase?stringtype=unspecified

mm9b1k5b

mm9b1k5b2#

在网上进行了一番调查之后,我找到了解决办法。由于postgresql无法确定数据类型,因此我们可以从以前将其声明为typedparametervalue。

TypedParameterValue attributeId = new TypedParameterValue(PostgresUUIDType.INSTANCE, UUIDUtil.toUUID(attributeId));
Page<Attribute> attributes = attributeRepo.findByAttributeId(attributeId);

然后在jpql中进行nullchecking,转换为org.hibernate.type.postgresuuidtype:
(在ide中,它可以显示为错误,但实际上可以编译)

@Query("select a from Attribute a where (cast(:attributeId as org.hibernate.type.PostgresUUIDType) OR a.id = :attributeId)")
 Page<Attribute> findByAttributeId(@Param("attributeId") TypedParameterValue attributeId);

在本机查询中:

@Query(value = "select * from attribute a where (cast(:attributeId as uuid) OR a.id = :attributeId)",nativeQuery = true)
 List<Attribute> findByAttributeId(@Param("attributeId") TypedParameterValue attributeId);

相关问题