简体   繁体   English

Hibernate getResultList()为计数查询返回重复项

[英]Hibernate getResultList() returning duplicates for a count query

I am running a count query using Java 1.6 and Hibernate version 3.2.1 that I expect to return a resultList containing [2]. 我正在使用Java 1.6和Hibernate版本3.2.1运行计数查询,我期望返回包含[2]的resultList。 I am instead getting a result list containing [2, 2, 2, 2]. 我得到一个包含[2,2,2,2]的结果列表。 I turned on debugging to see the hibernate queries allowing me to see the same query being run four times. 我打开调试看到hibernate查询,允许我看到相同的查询运行四次。

The ejbql on the Query I'm running getResultList() on: 查询上的ejbql我正在运行getResultList():

SELECT COUNT(*)FROM FlightLogItemEntity AS sectorItem  WHERE UPPER(sectorItem.sectorId) = :literal0 AND UPPER(sectorItem.logType) = :literal1

{literal0=0001006711, literal1=TL} {literal0 = 0001006711,literal1 = TL}

After running getResultList() only once, I see in my logs four identical queries being run: 在运行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_ 

At first I thought it might have to do with Entity relationships causing multiple queries to run, but after removing all relationships I still see the same duplicate queries being run. 起初我认为它可能与实体关系有关,导致运行多个查询,但在删除所有关系后,我仍然看到正在运行相同的重复查询。

The code generating the query (after setting the entityManager, ejbql, and Literals): 生成查询的代码(在设置entityManager,ejbql和Literals之后):

query.setFirstResult(-1);
query.setFlushMode(null);
query.setMaxResults(-1);
List<?> resultList = query.getResultList();

Does anyone know how I might prevent duplicate queries being run (to prevent duplicate results)? 有谁知道我如何防止重复查询运行(以防止重复结果)?

I'm not sure why its doing the query 4 times, but when you use query.setMaxResults(-1); 我不确定为什么它执行查询4次,但是当你使用query.setMaxResults(-1); you're setting it to return however many items it gets. 你设置它返回它获得的许多项目。 http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Query.html#setMaxResults(int) http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Query.html#setMaxResults(int)

Try changing query.setMaxResults(-1); 尝试更改query.setMaxResults(-1); to query.setMaxResults(1); to query.setMaxResults(1); This may just be a band-aid but it should allow you get just one result. 这可能只是一个创可贴,但它应该只允许你得到一个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM