Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以为null的jpql变量_Java_Hibernate_Jpa - Fatal编程技术网

Java 可以为null的jpql变量

Java 可以为null的jpql变量,java,hibernate,jpa,Java,Hibernate,Jpa,我在JpaRepository中有一个jpql查询,如下所示: @Query("select l from LogEntity l where l.codePackage = :codePackage and l.codeFile = :codeFile order by l.id desc") public Page<LogEntity> findSimilarAngularLog (@Param("codePackage") String codePackage, @Param(

我在JpaRepository中有一个jpql查询,如下所示:

@Query("select l from LogEntity l where l.codePackage = :codePackage and l.codeFile = :codeFile order by l.id desc")
public Page<LogEntity> findSimilarAngularLog (@Param("codePackage") String codePackage, @Param("codeFile") String codeFile, Pageable pageRequest);
摆脱它。因此,如果其中一个或两个都为NULL,则存在
l.codeFile=NULL
,而不是
l.codeFile为NULL
。然后他什么也没找到。
如果我将hibernate生成的sql字符串复制到MySQL控制台,并将
=NULL
更改为
IS NULL
,他将找到所有内容

那么,如何更改hibernate jpa的行为,以便在我的
@Query
字符串中正确处理空值


非常感谢和问候

您可以使用以下查询:

WHERE ((:param is null and t.field is null) or t.field = :param)
@Query("select l from LogEntity l where ((:codePackage is null and l.codePackage is null) or l.codePackage = :codePackage ) and ((:codeFile is null and l.codeFile is null) or l.codeFile = :codeFile ) order by l.id desc")
@Query("select l from LogEntity l where ((:codePackage is null and l.codePackage is null) or l.codePackage = :codePackage) and ((:codeFile is null and l.codeFile is null) or l.codeFile = :codeFile) order by l.id desc")
但是,通过逻辑而不是像使用命名查询那样使用hql,这难道不是一个好主意吗

更好的逻辑是使用if条件,比如

if(codePackage == null) {
    query += " l.codePackage is null ";
}
else {
    query += " l.codePackage = :codePackage ";
}
***code***
//query execution code

我使用的是JpaRepository接口,因此无法使用程序逻辑,因为函数实际上只是
@Query(…)公共对象(…参数…
)。还是我在监督什么?你一定有实体经理的目标。您可以使用实体管理器触发查询。否则,第一个解决方案可以随时实现。
if(codePackage == null) {
    query += " l.codePackage is null ";
}
else {
    query += " l.codePackage = :codePackage ";
}
***code***
//query execution code