我正在使用 Java 1.6 和 Hibernate 版本 3.2.1 运行计数查询,我希望返回包含 [2] 的结果列表。相反,我得到一个包含 [2, 2, 2, 2] 的结果列表。我打开调试以查看休眠查询,让我看到相同的查询运行了四次。
我正在运行 getResultList() 的查询上的 ejbql:
SELECT COUNT(*)FROM FlightLogItemEntity AS sectorItem WHERE UPPER(sectorItem.sectorId) = :literal0 AND UPPER(sectorItem.logType) = :literal1
{literal0=0001006711,literal1=TL}
仅运行一次 getResultList() 后,我在日志中看到正在运行四个相同的查询:
[SQL] select count(*) as col_0_0_ from SECTOR_ITEM techlogite0_ where upper(techlogite0_.SECTOR_ID)=? and upper(techlogite0_.LOG_TYPE)=?
[StringType] binding '0001006711' to parameter: 1
[StringType] binding 'TL' to parameter: 2
[LongType] returning '2' as column: col_0_0_
[SQL] select count(*) as col_0_0_ from SECTOR_ITEM flightlogi0_ where upper(flightlogi0_.SECTOR_ID)=? and upper(flightlogi0_.LOG_TYPE)=?
[StringType] binding '0001006711' to parameter: 1
[StringType] binding 'TL' to parameter: 2
[LongType] returning '2' as column: col_0_0_
[SQL] select count(*) as col_0_0_ from SECTOR_ITEM flightlogi0_ where upper(flightlogi0_.SECTOR_ID)=? and upper(flightlogi0_.LOG_TYPE)=?
[StringType] binding '0001006711' to parameter: 1
[StringType] binding 'TL' to parameter: 2
[LongType] returning '2' as column: col_0_0_
[SQL] select count(*) as col_0_0_ from SECTOR_ITEM cabinlogit0_ where upper(cabinlogit0_.SECTOR_ID)=? and upper(cabinlogit0_.LOG_TYPE)=?
[StringType] binding '0001006711' to parameter: 1
[StringType] binding 'TL' to parameter: 2
[LongType] returning '2' as column: col_0_0_
起初我认为这可能与导致多个查询运行的实体关系有关,但在删除所有关系后,我仍然看到正在运行相同的重复查询。
生成查询的代码(在设置 entityManager、ejbql 和 Literals 之后):
query.setFirstResult(-1);
query.setFlushMode(null);
query.setMaxResults(-1);
List<?> resultList = query.getResultList();
有谁知道我如何防止重复查询运行(以防止重复结果)?