Skip to content

Commit 7ec3430

Browse files
committed
fixes #556 Passing JDBC type when getting type handler for generated keys.
1 parent 98644db commit 7ec3430

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.ibatis.mapping.MappedStatement;
2727
import org.apache.ibatis.reflection.MetaObject;
2828
import org.apache.ibatis.session.Configuration;
29+
import org.apache.ibatis.type.JdbcType;
2930
import org.apache.ibatis.type.TypeHandler;
3031
import org.apache.ibatis.type.TypeHandlerRegistry;
3132

@@ -61,7 +62,7 @@ public void processBatch(MappedStatement ms, Statement stmt, Collection<Object>
6162
}
6263
final MetaObject metaParam = configuration.newMetaObject(parameter);
6364
if (typeHandlers == null) {
64-
typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties);
65+
typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
6566
}
6667
populateKeys(rs, metaParam, keyProperties, typeHandlers);
6768
}
@@ -100,12 +101,12 @@ private Collection<Object> getParameters(Object parameter) {
100101
return parameters;
101102
}
102103

103-
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties) {
104+
private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry, MetaObject metaParam, String[] keyProperties, ResultSetMetaData rsmd) throws SQLException {
104105
TypeHandler<?>[] typeHandlers = new TypeHandler<?>[keyProperties.length];
105106
for (int i = 0; i < keyProperties.length; i++) {
106107
if (metaParam.hasSetter(keyProperties[i])) {
107108
Class<?> keyPropertyType = metaParam.getSetterType(keyProperties[i]);
108-
TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType);
109+
TypeHandler<?> th = typeHandlerRegistry.getTypeHandler(keyPropertyType, JdbcType.forCode(rsmd.getColumnType(i + 1)));
109110
typeHandlers[i] = th;
110111
}
111112
}

src/test/java/org/apache/ibatis/submitted/typehandler/CreateDB.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
--
1616

1717
drop table users if exists;
18+
drop table product if exists;
1819

1920
create table users (
2021
id int,
@@ -23,4 +24,9 @@ create table users (
2324
state char(20)
2425
);
2526

27+
create table product (
28+
id int identity,
29+
name varchar(20)
30+
);
31+
2632
insert into users (id, name, city, state) values(1, ' User1', ' Carmel ', ' IN ');

src/test/java/org/apache/ibatis/submitted/typehandler/Mapper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.apache.ibatis.submitted.typehandler;
1717

18+
import java.util.Map;
19+
20+
import org.apache.ibatis.annotations.Insert;
21+
import org.apache.ibatis.annotations.Options;
1822
import org.apache.ibatis.annotations.Result;
1923
import org.apache.ibatis.annotations.Results;
2024
import org.apache.ibatis.annotations.Select;
@@ -30,4 +34,10 @@ public interface Mapper {
3034
@Result(column="state", property="state", jdbcType=JdbcType.VARCHAR)
3135
})
3236
User getUser(Integer id);
37+
38+
@Insert({
39+
"insert into product (name) values (#{name})"
40+
})
41+
@Options(useGeneratedKeys = true, keyProperty = "id")
42+
int insertProduct(Product product);
3343
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.submitted.typehandler;
18+
19+
import java.sql.CallableStatement;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
24+
import org.apache.ibatis.type.BaseTypeHandler;
25+
import org.apache.ibatis.type.JdbcType;
26+
27+
public class Product {
28+
29+
private ProductId id;
30+
31+
private String name;
32+
33+
public ProductId getId() {
34+
return id;
35+
}
36+
37+
public void setId(ProductId id) {
38+
this.id = id;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public static class ProductId {
50+
private Integer value;
51+
52+
private ProductId(Integer value) {
53+
super();
54+
this.value = value;
55+
}
56+
57+
public Integer getValue() {
58+
return value;
59+
}
60+
}
61+
62+
public static class ProductIdTypeHandler extends BaseTypeHandler<ProductId> {
63+
@Override
64+
public void setNonNullParameter(PreparedStatement ps, int i, ProductId parameter, JdbcType jdbcType) throws SQLException {
65+
ps.setInt(i, parameter.getValue());
66+
}
67+
68+
@Override
69+
public ProductId getNullableResult(ResultSet rs, String columnName) throws SQLException {
70+
return new ProductId(rs.getInt(columnName));
71+
}
72+
73+
@Override
74+
public ProductId getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
75+
return new ProductId(rs.getInt(columnIndex));
76+
}
77+
78+
@Override
79+
public ProductId getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
80+
return new ProductId(cs.getInt(columnIndex));
81+
}
82+
}
83+
}

src/test/java/org/apache/ibatis/submitted/typehandler/TypeHandlerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.apache.ibatis.submitted.typehandler;
1717

18+
import static org.junit.Assert.*;
19+
1820
import java.io.Reader;
1921
import java.sql.Connection;
22+
import java.util.HashMap;
23+
import java.util.Map;
2024

2125
import org.apache.ibatis.io.Resources;
2226
import org.apache.ibatis.jdbc.ScriptRunner;
@@ -65,4 +69,18 @@ public void shouldGetAUser() {
6569
}
6670
}
6771

72+
@Test
73+
public void shouldApplyTypeHandlerOnGeneratedKey() {
74+
SqlSession sqlSession = sqlSessionFactory.openSession();
75+
try {
76+
Mapper mapper = sqlSession.getMapper(Mapper.class);
77+
Product product = new Product();
78+
product.setName("new product");
79+
mapper.insertProduct(product);
80+
assertNotNull(product.getId());
81+
assertNotNull(product.getId().getValue());
82+
} finally {
83+
sqlSession.close();
84+
}
85+
}
6886
}

src/test/java/org/apache/ibatis/submitted/typehandler/mybatis-config.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
<configuration>
2222

23+
<typeHandlers>
24+
<typeHandler
25+
handler="org.apache.ibatis.submitted.typehandler.Product$ProductIdTypeHandler"
26+
javaType="org.apache.ibatis.submitted.typehandler.Product$ProductId"
27+
jdbcType="INTEGER" />
28+
</typeHandlers>
29+
2330
<environments default="development">
2431
<environment id="development">
2532
<transactionManager type="JDBC">

0 commit comments

Comments
 (0)