From fc8c6d25c1e8266221bf0a9054f2233672cddd99 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Mon, 29 Sep 2014 15:12:34 +0900 Subject: [PATCH 1/2] Fixes #273 An extra call to ResultHandler#handleResult is made for a nested result map. --- .../resultset/DefaultResultSetHandler.java | 10 +- .../Account.java | 61 ++++++++++ .../AccountAddress.java | 49 ++++++++ .../AccountRepository.xml | 48 ++++++++ .../CreateDB.sql | 37 ++++++ .../NestedResultHandlerAssociationTest.java | 106 ++++++++++++++++++ .../mybatis-config.xml | 40 +++++++ 7 files changed, 346 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/Account.java create mode 100644 src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountAddress.java create mode 100644 src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/NestedResultHandlerAssociationTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml diff --git a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java index c7086e0a4a..52d7b5d822 100644 --- a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java +++ b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java @@ -289,7 +289,7 @@ private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap r throws SQLException { DefaultResultContext resultContext = new DefaultResultContext(); skipRows(rsw.getResultSet(), rowBounds); - while (shouldProcessMoreRows(rsw.getResultSet(), resultContext, rowBounds)) { + while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) { ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null); Object rowValue = getRowValue(rsw, discriminatedResultMap); storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet()); @@ -309,8 +309,8 @@ private void callResultHandler(ResultHandler resultHandler, DefaultResultContext resultHandler.handleResult(resultContext); } - private boolean shouldProcessMoreRows(ResultSet rs, ResultContext context, RowBounds rowBounds) throws SQLException { - return !context.isStopped() && rs.next() && context.getResultCount() < rowBounds.getLimit(); + private boolean shouldProcessMoreRows(ResultContext context, RowBounds rowBounds) throws SQLException { + return !context.isStopped() && context.getResultCount() < rowBounds.getLimit(); } private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException { @@ -755,7 +755,7 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r final DefaultResultContext resultContext = new DefaultResultContext(); skipRows(rsw.getResultSet(), rowBounds); Object rowValue = null; - while (shouldProcessMoreRows(rsw.getResultSet(), resultContext, rowBounds)) { + while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) { final ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null); final CacheKey rowKey = createRowKey(discriminatedResultMap, rsw, null); Object partialObject = nestedResultObjects.get(rowKey); @@ -773,7 +773,7 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r } } } - if (rowValue != null && mappedStatement.isResultOrdered()) { + if (rowValue != null && mappedStatement.isResultOrdered() && shouldProcessMoreRows(resultContext, rowBounds)) { storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet()); } } diff --git a/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/Account.java b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/Account.java new file mode 100644 index 0000000000..356a1bb62f --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/Account.java @@ -0,0 +1,61 @@ +/*- + * Copyright 2009-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ibatis.submitted.nestedresulthandler_association; + +import java.util.Date; + +public class Account { + private String accountUuid; + + private String accountName; + + private Date birthDate; + + private AccountAddress address; + + public String getAccountUuid() { + return accountUuid; + } + + public void setAccountUuid(String accountUuid) { + this.accountUuid = accountUuid; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public Date getBirthDate() { + return birthDate; + } + + public void setBirthDate(Date birthDate) { + this.birthDate = birthDate; + } + + public AccountAddress getAddress() { + return address; + } + + public void setAddress(AccountAddress address) { + this.address = address; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountAddress.java b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountAddress.java new file mode 100644 index 0000000000..2e8b9c8ba2 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountAddress.java @@ -0,0 +1,49 @@ +/*- + * Copyright 2009-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ibatis.submitted.nestedresulthandler_association; + +public class AccountAddress { + private String accountUuid; + + private String zipCode; + + private String address; + + public String getAccountUuid() { + return accountUuid; + } + + public void setAccountUuid(String accountUuid) { + this.accountUuid = accountUuid; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.xml b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.xml new file mode 100644 index 0000000000..b917c9ad86 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql new file mode 100644 index 0000000000..394f1f2aa9 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql @@ -0,0 +1,37 @@ +-- +-- Copyright 2009-2014 the original author or authors. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +DROP TABLE t_account_address if exists; +DROP TABLE t_account if exists; +CREATE TABLE t_account( + account_uuid CHAR(36) PRIMARY KEY + ,account_name VARCHAR(256) + ,birth_date DATETIME +); +CREATE TABLE t_account_address( + account_uuid CHAR(36) PRIMARY KEY + ,zip_code CHAR(7) + ,address VARCHAR(256) +); + +INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc01', 'Bob1', '2014-01-01 00:00:00.000'); +INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc02', 'Bob2', '2014-01-02 00:00:00.000'); +INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc03', 'Bob3', '2014-01-03 00:00:00.000'); +INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc04', 'Bob4', '2014-01-04 00:00:00.000'); +INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc05', 'Bob5', '2014-01-05 00:00:00.000'); +INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc11', 'Mark1', '2014-02-01 00:00:00.000'); + +INSERT INTO t_account_address (account_uuid,zip_code,address) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc02', '1710051', 'Tokyo Toshimaku Nagasaki'); diff --git a/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/NestedResultHandlerAssociationTest.java b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/NestedResultHandlerAssociationTest.java new file mode 100644 index 0000000000..328011a997 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/NestedResultHandlerAssociationTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2009-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.submitted.nestedresulthandler_association; + +import static org.junit.Assert.*; + +import java.io.Reader; +import java.sql.Connection; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.BeforeClass; +import org.junit.Test; + +public class NestedResultHandlerAssociationTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // create an SqlSessionFactory + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml"); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + reader.close(); + + // populate in-memory database + SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql"); + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + reader.close(); + session.close(); + } + + @Test + public void shouldHandleRowBounds() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); + Date targetMonth = fmt.parse("2014-01-01"); + final List accounts = new ArrayList(); + try { + sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() { + @Override + public void handleResult(ResultContext context) { + Account account = (Account) context.getResultObject(); + accounts.add(account); + } + }); + } finally { + sqlSession.close(); + } + assertEquals(2, accounts.size()); + assertEquals("Bob2", accounts.get(0).getAccountName()); + assertEquals("Bob3", accounts.get(1).getAccountName()); + } + + @Test + public void shouldHandleStop() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); + final List accounts = new ArrayList(); + try { + Date targetMonth = fmt.parse("2014-01-01"); + sqlSession.select("collectPageByBirthMonth", targetMonth, new ResultHandler() { + @Override + public void handleResult(ResultContext context) { + Account account = (Account) context.getResultObject(); + accounts.add(account); + if (accounts.size() > 1) + context.stop(); + } + }); + } finally { + sqlSession.close(); + } + assertEquals(2, accounts.size()); + assertEquals("Bob1", accounts.get(0).getAccountName()); + assertEquals("Bob2", accounts.get(1).getAccountName()); + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml new file mode 100644 index 0000000000..a2f5a8b786 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + From 5c3fa3e82ae02140ccf253b8e3ec74cb49e2dbb6 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Thu, 9 Oct 2014 11:13:38 +0900 Subject: [PATCH 2/2] Added JDK8 build on travis-ci. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c6044a8c68..5938768559 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: java jdk: + - oraclejdk8 - oraclejdk7 - openjdk7 - openjdk6