Skip to content

Commit 8079262

Browse files
author
Thomas Risberg
committedJun 2, 2009
Updated BeanPropertyRowMapper to remove spaces from column names to improve matching to properties (SPR-5758)
1 parent 3192b92 commit 8079262

File tree

4 files changed

+167
-4
lines changed

4 files changed

+167
-4
lines changed
 

‎org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
236236
Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null);
237237

238238
for (int index = 1; index <= columnCount; index++) {
239-
String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
240-
PropertyDescriptor pd = this.mappedFields.get(column);
239+
String column = JdbcUtils.lookupColumnName(rsmd, index);
240+
PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase());
241241
if (pd != null) {
242242
try {
243243
Object value = getColumnValue(rs, index, pd);

‎org.springframework.jdbc/src/test/java/org/springframework/jdbc/core/AbstractRowMapperTests.java

+90-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import org.springframework.jdbc.core.test.ConcretePerson;
3333
import org.springframework.jdbc.core.test.Person;
34+
import org.springframework.jdbc.core.test.SpacePerson;
3435
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
3536
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
3637

@@ -48,13 +49,17 @@ public abstract class AbstractRowMapperTests extends TestCase {
4849
protected Connection con;
4950
protected MockControl conControl2;
5051
protected Connection con2;
52+
protected MockControl conControl3;
53+
protected Connection con3;
54+
5155
protected MockControl rsmdControl;
5256
protected ResultSetMetaData rsmd;
5357
protected MockControl rsControl;
5458
protected ResultSet rs;
5559
protected MockControl stmtControl;
5660
protected Statement stmt;
5761
protected JdbcTemplate jdbcTemplate;
62+
5863
protected MockControl rsmdControl2;
5964
protected ResultSetMetaData rsmd2;
6065
protected MockControl rsControl2;
@@ -63,6 +68,14 @@ public abstract class AbstractRowMapperTests extends TestCase {
6368
protected Statement stmt2;
6469
protected JdbcTemplate jdbcTemplate2;
6570

71+
protected MockControl rsmdControl3;
72+
protected ResultSetMetaData rsmd3;
73+
protected MockControl rsControl3;
74+
protected ResultSet rs3;
75+
protected MockControl stmtControl3;
76+
protected Statement stmt3;
77+
protected JdbcTemplate jdbcTemplate3;
78+
6679
protected void setUp() throws SQLException {
6780
conControl = MockControl.createControl(Connection.class);
6881
con = (Connection) conControl.getMock();
@@ -119,6 +132,9 @@ protected void setUp() throws SQLException {
119132
stmt.close();
120133
stmtControl.setVoidCallable(1);
121134

135+
conControl.replay();
136+
stmtControl.replay();
137+
122138
conControl2 = MockControl.createControl(Connection.class);
123139
con2 = (Connection) conControl2.getMock();
124140
con2.isClosed();
@@ -174,11 +190,67 @@ protected void setUp() throws SQLException {
174190
stmt2.close();
175191
stmtControl2.setVoidCallable(2);
176192

177-
conControl.replay();
178-
stmtControl.replay();
179193
conControl2.replay();
180194
stmtControl2.replay();
181195

196+
conControl3 = MockControl.createControl(Connection.class);
197+
con3 = (Connection) conControl3.getMock();
198+
con3.isClosed();
199+
conControl3.setDefaultReturnValue(false);
200+
201+
rsmdControl3 = MockControl.createControl(ResultSetMetaData.class);
202+
rsmd3 = (ResultSetMetaData)rsmdControl3.getMock();
203+
rsmd3.getColumnCount();
204+
rsmdControl3.setReturnValue(4, 1);
205+
rsmd3.getColumnLabel(1);
206+
rsmdControl3.setReturnValue("Last Name", 1);
207+
rsmd3.getColumnLabel(2);
208+
rsmdControl3.setReturnValue("age", 1);
209+
rsmd3.getColumnLabel(3);
210+
rsmdControl3.setReturnValue("birth_date", 1);
211+
rsmd3.getColumnLabel(4);
212+
rsmdControl3.setReturnValue("balance", 1);
213+
rsmdControl3.replay();
214+
215+
rsControl3 = MockControl.createControl(ResultSet.class);
216+
rs3 = (ResultSet) rsControl3.getMock();
217+
rs3.getMetaData();
218+
rsControl3.setReturnValue(rsmd3, 1);
219+
rs3.next();
220+
rsControl3.setReturnValue(true, 1);
221+
rs3.getString(1);
222+
rsControl3.setReturnValue("Gagarin", 1);
223+
rs3.wasNull();
224+
rsControl3.setReturnValue(false, 1);
225+
rs3.getLong(2);
226+
rsControl3.setReturnValue(22, 1);
227+
rs3.getTimestamp(3);
228+
rsControl3.setReturnValue(new Timestamp(1221222L), 1);
229+
rs3.getBigDecimal(4);
230+
rsControl3.setReturnValue(new BigDecimal("1234.56"), 1);
231+
rs3.next();
232+
rsControl3.setReturnValue(false, 1);
233+
rs3.close();
234+
rsControl3.setVoidCallable(1);
235+
rsControl3.replay();
236+
237+
stmtControl3 = MockControl.createControl(Statement.class);
238+
stmt3 = (Statement) stmtControl3.getMock();
239+
240+
con3.createStatement();
241+
conControl3.setReturnValue(stmt3, 1);
242+
stmt3.executeQuery("select last_name as \"Last Name\", age, birth_date, balance from people");
243+
stmtControl3.setReturnValue(rs3, 1);
244+
if (debugEnabled) {
245+
stmt3.getWarnings();
246+
stmtControl3.setReturnValue(null, 1);
247+
}
248+
stmt3.close();
249+
stmtControl3.setVoidCallable(1);
250+
251+
conControl3.replay();
252+
stmtControl3.replay();
253+
182254
jdbcTemplate = new JdbcTemplate();
183255
jdbcTemplate.setDataSource(new SingleConnectionDataSource(con, false));
184256
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
@@ -188,6 +260,11 @@ protected void setUp() throws SQLException {
188260
jdbcTemplate2.setDataSource(new SingleConnectionDataSource(con2, false));
189261
jdbcTemplate2.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
190262
jdbcTemplate2.afterPropertiesSet();
263+
264+
jdbcTemplate3 = new JdbcTemplate();
265+
jdbcTemplate3.setDataSource(new SingleConnectionDataSource(con3, false));
266+
jdbcTemplate3.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
267+
jdbcTemplate3.afterPropertiesSet();
191268
}
192269

193270
protected void verifyPerson(Person bean) {
@@ -217,6 +294,17 @@ protected void verifyConcretePerson(ConcretePerson bean) {
217294
assertEquals(new BigDecimal("1234.56"), bean.getBalance());
218295
}
219296

297+
protected void verifySpacePerson(SpacePerson bean) {
298+
conControl3.verify();
299+
rsControl3.verify();
300+
rsmdControl3.verify();
301+
stmtControl3.verify();
302+
assertEquals("Gagarin", bean.getLastName());
303+
assertEquals(22L, bean.getAge());
304+
assertEquals(new java.util.Date(1221222L), bean.getBirthDate());
305+
assertEquals(new BigDecimal("1234.56"), bean.getBalance());
306+
}
307+
220308
private void verify() {
221309
conControl.verify();
222310
rsControl.verify();

‎org.springframework.jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.jdbc.core.test.ConcretePerson;
2424
import org.springframework.jdbc.core.test.ExtendedPerson;
2525
import org.springframework.jdbc.core.test.Person;
26+
import org.springframework.jdbc.core.test.SpacePerson;
2627
import org.springframework.beans.TypeMismatchException;
2728

2829
/**
@@ -108,4 +109,12 @@ public void testMappingNullValue() throws SQLException {
108109
verifyPersonWithZeroAge(bean);
109110
}
110111

112+
public void testQueryWithSpaceInColumnName() throws SQLException {
113+
List result = jdbcTemplate3.query("select last_name as \"Last Name\", age, birth_date, balance from people",
114+
new BeanPropertyRowMapper(SpacePerson.class));
115+
assertEquals(1, result.size());
116+
SpacePerson bean = (SpacePerson) result.get(0);
117+
verifySpacePerson(bean);
118+
}
119+
111120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2008 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.springframework.jdbc.core.test;
18+
19+
import java.math.BigDecimal;
20+
21+
/**
22+
* @author Thomas Risberg
23+
*/
24+
public class SpacePerson {
25+
26+
private String lastName;
27+
28+
private long age;
29+
30+
private java.util.Date birthDate;
31+
32+
private BigDecimal balance;
33+
34+
public String getLastName() {
35+
return lastName;
36+
}
37+
38+
public void setLastName(String lastName) {
39+
this.lastName = lastName;
40+
}
41+
42+
public long getAge() {
43+
return age;
44+
}
45+
46+
public void setAge(long age) {
47+
this.age = age;
48+
}
49+
50+
public java.util.Date getBirthDate() {
51+
return birthDate;
52+
}
53+
54+
public void setBirth_date(java.util.Date birthDate) {
55+
this.birthDate = birthDate;
56+
}
57+
58+
public BigDecimal getBalance() {
59+
return balance;
60+
}
61+
62+
public void setBalance(BigDecimal balanace) {
63+
this.balance = balanace;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)
Please sign in to comment.