Skip to content

Commit 93197bd

Browse files
committed
fixes #107 Auto-mapping + columnPrefix fails under case-sensitive database engines. Normalize unmapped column names to uppercase before searching columnPrefix.
1 parent 8bfd5a2 commit 93197bd

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap
402402
if (columnPrefix != null && columnPrefix.length() > 0) {
403403
// When columnPrefix is specified,
404404
// ignore columns without the prefix.
405-
if (columnName.startsWith(columnPrefix)) {
405+
if (columnName.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
406406
propertyName = columnName.substring(columnPrefix.length());
407407
} else {
408408
continue;
@@ -913,7 +913,7 @@ private void createRowKeyForUnmappedProperties(ResultMap resultMap, ResultSetWra
913913
if (columnPrefix != null && columnPrefix.length() > 0) {
914914
// When columnPrefix is specified,
915915
// ignore columns without the prefix.
916-
if (column.startsWith(columnPrefix)) {
916+
if (column.toUpperCase(Locale.ENGLISH).startsWith(columnPrefix)) {
917917
property = column.substring(columnPrefix.length());
918918
} else {
919919
continue;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2009-2013 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.column_prefix;
18+
19+
import java.util.List;
20+
21+
public class Brand {
22+
private Integer id;
23+
24+
private String name;
25+
26+
private List<Product> products;
27+
28+
public Integer getId() {
29+
return id;
30+
}
31+
32+
public void setId(Integer id) {
33+
this.id = id;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
public List<Product> getProducts() {
45+
return products;
46+
}
47+
48+
public void setProducts(List<Product> products) {
49+
this.products = products;
50+
}
51+
}

src/test/java/org/apache/ibatis/submitted/column_prefix/ColumnPrefixAutoMappingTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package org.apache.ibatis.submitted.column_prefix;
1818

19+
import static org.junit.Assert.*;
20+
1921
import java.util.List;
2022

2123
import org.apache.ibatis.session.SqlSession;
24+
import org.junit.Test;
2225

2326
public class ColumnPrefixAutoMappingTest extends ColumnPrefixTest {
2427
protected List<Pet> getPetAndRoom(SqlSession sqlSession) {
@@ -35,4 +38,18 @@ protected List<Person> getPersons(SqlSession sqlSession) {
3538
protected String getConfigPath() {
3639
return "org/apache/ibatis/submitted/column_prefix/ConfigAutoMapping.xml";
3740
}
41+
42+
@Test
43+
public void testCaseInsensitivity() throws Exception {
44+
SqlSession sqlSession = sqlSessionFactory.openSession();
45+
try {
46+
Brand brand = sqlSession.selectOne("org.apache.ibatis.submitted.column_prefix.MapperAutoMapping.selectBrandWithProducts", 1);
47+
assertEquals(Integer.valueOf(1), brand.getId());
48+
assertEquals(2, brand.getProducts().size());
49+
assertEquals(Integer.valueOf(10), brand.getProducts().get(0).getId());
50+
assertEquals("alpha", brand.getProducts().get(0).getName());
51+
} finally {
52+
sqlSession.close();
53+
}
54+
}
3855
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ room_id int,
6868
room_name varchar(32)
6969
);
7070

71+
drop table if exists brand;
72+
create table brand (
73+
id int,
74+
name varchar(32)
75+
);
76+
77+
-- make columns case sensitive
78+
drop table if exists product;
79+
create table product (
80+
"product_id" int,
81+
"product_name" varchar(32),
82+
brand_id int
83+
);
84+
7185
insert into room (room_id, room_name) values (31, 'Sakura');
7286
insert into room (room_id, room_name) values (32, 'Ume');
7387
insert into room (room_id, room_name) values (33, 'Tsubaki');
@@ -98,3 +112,8 @@ insert into address (id, state, city, phone1_id, phone2_id, addr_type, caution)
98112
insert into person (id, name, billing_address_id, shipping_address_id, room_id) values (1, 'John', 10, 11, 33);
99113
insert into person (id, name, billing_address_id, shipping_address_id, room_id) values (2, 'Rebecca', 12, null, null);
100114
insert into person (id, name, billing_address_id, shipping_address_id, room_id) values (3, 'Keith', null, 13, null);
115+
116+
insert into brand (id, name) values (1, 'alpha');
117+
118+
insert into product ("product_id", "product_name", brand_id) values (10, 'alpha', 1);
119+
insert into product ("product_id", "product_name", brand_id) values (20, 'beta', 1);

src/test/java/org/apache/ibatis/submitted/column_prefix/MapperAutoMapping.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,23 @@
158158
order by person.id, pet.id
159159
]]></select>
160160

161+
<resultMap id="productMapper"
162+
type="org.apache.ibatis.submitted.column_prefix.Product">
163+
<id column="id" property="id" />
164+
</resultMap>
165+
166+
<resultMap id="brandMapper"
167+
type="org.apache.ibatis.submitted.column_prefix.Brand">
168+
<id column="id" property="id" />
169+
<collection property="products" resultMap="productMapper" columnPrefix="product_" />
170+
</resultMap>
171+
172+
<select id="selectBrandWithProducts" resultMap="brandMapper">
173+
select brand.id, brand.name, product."product_id" as "product_id", product."product_name"
174+
from brand
175+
left join product on product.brand_id = brand.id
176+
where brand.id = #{id}
177+
order by product."product_id"
178+
</select>
179+
161180
</mapper>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2009-2013 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.column_prefix;
18+
19+
public class Product {
20+
private Integer id;
21+
22+
private String name;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
}

0 commit comments

Comments
 (0)