简体   繁体   中英

mybatis not returning all the rows on lazy fetch

My java application when using mybatis (3.4.4) to query oracle back end is not returning all the rows when using lazy load.

Essentially,

  • when I query my database using some SQL tool (like oracle Sql Developer) I get 4000 results

  • when I query using selectCursor which leads to lazy load conn.selectCusror(query) I only get 560 result!

  • when I query using selectList which will fetch all result at once conn.selectList(query) I am getting 4000 results (matches with database)

Note : A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. Documentation

This is how I am counting the number of records

Cursor<Object> cur = conn.selectCursor(query) ; // query definition is written below
int count =0;
for(Object ob : cur){
    count++;
}
System.out.println(count);

QUERY

<select id="query" fetchSize="20000" resultType="java.util.Map" >
    select distinct code from my CODES_VIEW
</select>

Can someone please advise why selectCursor is not giving all the 4000 results

After spending days I found this solution:

My result set from the query contains a row (561) which have null values in its column. This shouldn't be a problem as I can have null values in the columns I fetched . In this case code column has value null at row 561. When fetching records via selectCursor (lazy method of fetching data) mybatis believes it is end of record when all the columns are null in the fetched row.

MyBatis, by default, returns null when all the columns of a returned row are NULL. When this setting is enabled, MyBatis returns an empty instance instead. Note that it is also applied to nested results (ie collectioin and association). Since: 3.4.2 https://mybatis.org/mybatis-3/configuration.html

However, you won't face this problem when you use selectList as all the records are fetched at once and cursor doesn't keep on checking on how does the next fetched records look like .

Hence the solution is to put the below setting in the mybatis config.xml

<setting name="returnInstanceForEmptyRow" value="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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