From fba7af05e10e03b8960ff12bb8d97852d11abd3a Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Sun, 13 Aug 2017 02:31:11 +0900 Subject: [PATCH] Reverted #782 as a workaround for #902 . --- .../executor/keygen/Jdbc3KeyGenerator.java | 13 +- .../keygen/Jdbc3KeyGeneratorTest.java | 2 + .../usesjava8/postgres_genkeys/CreateDB.sql | 34 +++++ .../usesjava8/postgres_genkeys/Mapper.java | 31 +++++ .../PostgresGeneratedKeysTest.java | 116 ++++++++++++++++++ .../usesjava8/postgres_genkeys/Section.java | 39 ++++++ .../usesjava8/postgres_genkeys/User.java | 38 ++++++ 7 files changed, 268 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java create mode 100644 src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java diff --git a/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java b/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java index 68368634490..fd6ccdf3745 100644 --- a/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java +++ b/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; +import org.apache.ibatis.binding.BindingException; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.ExecutorException; import org.apache.ibatis.mapping.MappedStatement; @@ -116,8 +117,13 @@ private TypeHandler[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry TypeHandler[] typeHandlers = new TypeHandler[keyProperties.length]; for (int i = 0; i < keyProperties.length; i++) { if (metaParam.hasSetter(keyProperties[i])) { - Class keyPropertyType = metaParam.getSetterType(keyProperties[i]); - TypeHandler th = typeHandlerRegistry.getTypeHandler(keyPropertyType, JdbcType.forCode(rsmd.getColumnType(i + 1))); + TypeHandler th; + try { + Class keyPropertyType = metaParam.getSetterType(keyProperties[i]); + th = typeHandlerRegistry.getTypeHandler(keyPropertyType, JdbcType.forCode(rsmd.getColumnType(i + 1))); + } catch (BindingException e) { + th = null; + } typeHandlers[i] = th; } } @@ -127,9 +133,6 @@ private TypeHandler[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler[] typeHandlers) throws SQLException { for (int i = 0; i < keyProperties.length; i++) { String property = keyProperties[i]; - if (!metaParam.hasSetter(property)) { - throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + "."); - } TypeHandler th = typeHandlers[i]; if (th != null) { Object value = th.getResult(rs, i + 1); diff --git a/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java b/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java index d7e1dca66cd..003ef16e7b6 100644 --- a/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java +++ b/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java @@ -29,6 +29,7 @@ import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import static com.googlecode.catchexception.apis.BDDCatchException.*; @@ -79,6 +80,7 @@ public void shouldInsertListAndRetrieveId() throws Exception { } } + @Ignore("#782 was reverted. See #902.") @Test public void shouldErrorUndefineProperty() { SqlSession sqlSession = sqlSessionFactory.openSession(); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql new file mode 100644 index 00000000000..8b36b091b97 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql @@ -0,0 +1,34 @@ +-- +-- Copyright 2009-2017 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. +-- + +CREATE SCHEMA mbtest; + +CREATE TABLE mbtest.users ( + user_id serial PRIMARY KEY, + name character varying(30) +); + +INSERT INTO mbtest.users (name) values +('Jimmy'); + + +CREATE TABLE mbtest.sections ( + section_id int PRIMARY KEY, + name character varying(30) +); + +INSERT INTO mbtest.sections (section_id, name) values +(1, 'Section 1'); diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java new file mode 100644 index 00000000000..f43b4c93581 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Mapper.java @@ -0,0 +1,31 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Update; + +public interface Mapper { + @Insert("insert into mbtest.sections (section_id, name) values (#{sectionId}, #{name})") + int insertSection(Section section); + + @Update("update mbtest.users set name = #{name} where user_id = #{userId}") + int updateUser(User user); + + @Insert("insert into mbtest.users (name) values (#{name})") + int insertUser(@Param("name") String name); +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java new file mode 100644 index 00000000000..ffa64a95da7 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/PostgresGeneratedKeysTest.java @@ -0,0 +1,116 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +import static org.junit.Assert.*; + +import java.io.Reader; +import java.nio.file.Paths; +import java.sql.Connection; +import java.util.Collections; + +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.submitted.usesjava8.keycolumn.InsertMapper; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres; +import ru.yandex.qatools.embed.postgresql.util.SocketUtil; + +public class PostgresGeneratedKeysTest { + + private static final EmbeddedPostgres postgres = new EmbeddedPostgres(); + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // Launch PostgreSQL server. Download / unarchive if necessary. + String url = postgres.start(EmbeddedPostgres.cachedRuntimeConfig(Paths.get("target/postgres")), "localhost", SocketUtil.findFreePort(), "postgres_genkeys", "postgres", "root", Collections.emptyList()); + + Configuration configuration = new Configuration(); + Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource( + "org.postgresql.Driver", url, null)); + configuration.setEnvironment(environment); + configuration.setUseGeneratedKeys(true); + configuration.addMapper(Mapper.class); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); + + try (SqlSession session = sqlSessionFactory.openSession(); + Connection conn = session.getConnection(); + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/usesjava8/postgres_genkeys/CreateDB.sql")) { + ScriptRunner runner = new ScriptRunner(conn); + runner.setLogWriter(null); + runner.runScript(reader); + } + } + + @AfterClass + public static void tearDown() { + postgres.stop(); + } + + @Test + public void testInsertIntoTableWithNoSerialColumn() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + Section section = new Section(); + section.setSectionId(2); + section.setName("Section 2"); + int result = mapper.insertSection(section); + assertEquals(1, result); + } finally { + sqlSession.close(); + } + } + + @Test + public void testUpdateTableWithSerial() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + User user = new User(); + user.setUserId(1); + user.setName("Ethan"); + int result = mapper.updateUser(user); + assertEquals(1, result); + } finally { + sqlSession.close(); + } + } + + @Test + public void testUnusedGeneratedKey() throws Exception { + SqlSession sqlSession = sqlSessionFactory.openSession(); + try { + Mapper mapper = sqlSession.getMapper(Mapper.class); + int result = mapper.insertUser("John"); + assertEquals(1, result); + } finally { + sqlSession.close(); + } + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java new file mode 100644 index 00000000000..235ed30c38b --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/Section.java @@ -0,0 +1,39 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +public class Section { + private Integer sectionId; + + private String name; + + public Integer getSectionId() { + return sectionId; + } + + public void setSectionId(Integer sectionId) { + this.sectionId = sectionId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java new file mode 100644 index 00000000000..5e3a827812f --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/usesjava8/postgres_genkeys/User.java @@ -0,0 +1,38 @@ +/** + * Copyright 2009-2017 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.usesjava8.postgres_genkeys; + +public class User { + private Integer userId; + + private String name; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}