簡體   English   中英

Hibernate getResultList()為計數查詢返回重復項

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

我正在使用Java 1.6和Hibernate版本3.2.1運行計數查詢,我期望返回包含[2]的resultList。 我得到一個包含[2,2,2,2]的結果列表。 我打開調試看到hibernate查詢,允許我看到相同的查詢運行四次。

查詢上的ejbql我正在運行getResultList():

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();

有誰知道我如何防止重復查詢運行(以防止重復結果)?

我不確定為什么它執行查詢4次,但是當你使用query.setMaxResults(-1); 你設置它返回它獲得的許多項目。 http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Query.html#setMaxResults(int)

嘗試更改query.setMaxResults(-1); to query.setMaxResults(1); 這可能只是一個創可貼,但它應該只允許你得到一個結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM