Skip to content

Commit 9a2f9cc

Browse files
committed
added static newInstance method to BeanPropertyRowMapper (SPR-6433); aligned ParameterizedBeanPropertyRowMapper factory methods
1 parent fce0361 commit 9a2f9cc

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,6 +100,8 @@ public BeanPropertyRowMapper() {
100100
/**
101101
* Create a new BeanPropertyRowMapper, accepting unpopulated properties
102102
* in the target bean.
103+
* <p>Consider using the {@link #newInstance} factory method instead,
104+
* which allows for specifying the mapped type once only.
103105
* @param mappedClass the class that each row should be mapped to
104106
*/
105107
public BeanPropertyRowMapper(Class<T> mappedClass) {
@@ -204,22 +206,23 @@ public boolean isCheckFullyPopulated() {
204206
}
205207

206208
/**
207-
* Set whether we're defaulting Java primitives in the case of mapping a null value from corresponding
208-
* database fields.
209+
* Set whether we're defaulting Java primitives in the case of mapping a null value
210+
* from corresponding database fields.
209211
* <p>Default is <code>false</code>, throwing an exception when nulls are mapped to Java primitives.
210212
*/
211-
public boolean isPrimitivesDefaultedForNullValue() {
212-
return primitivesDefaultedForNullValue;
213+
public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) {
214+
this.primitivesDefaultedForNullValue = primitivesDefaultedForNullValue;
213215
}
214216

215217
/**
216-
* Return whether we're defaulting Java primitives in the case of mapping a null value from corresponding
217-
* database fields.
218+
* Return whether we're defaulting Java primitives in the case of mapping a null value
219+
* from corresponding database fields.
218220
*/
219-
public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) {
220-
this.primitivesDefaultedForNullValue = primitivesDefaultedForNullValue;
221+
public boolean isPrimitivesDefaultedForNullValue() {
222+
return primitivesDefaultedForNullValue;
221223
}
222224

225+
223226
/**
224227
* Extract the values for all columns in the current row.
225228
* <p>Utilizes public setters and result set metadata.
@@ -305,4 +308,16 @@ protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd)
305308
return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
306309
}
307310

311+
312+
/**
313+
* Static factory method to create a new BeanPropertyRowMapper
314+
* (with the mapped class specified only once).
315+
* @param mappedClass the class that each row should be mapped to
316+
*/
317+
public static <T> BeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
318+
BeanPropertyRowMapper<T> newInstance = new BeanPropertyRowMapper<T>();
319+
newInstance.setMappedClass(mappedClass);
320+
return newInstance;
321+
}
322+
308323
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/ParameterizedBeanPropertyRowMapper.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,9 +35,9 @@
3535
* String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long,
3636
* float, Float, double, Double, BigDecimal, <code>java.util.Date</code>, etc.
3737
*
38-
* <p>The mapper can be configured to use the primitives default value when mapping null values by
39-
* passing in 'true' for the 'primitivesDefaultedForNullValue' using the {@link #newInstance(Class, boolean)} method.
40-
* Also see {@link BeanPropertyRowMapper#setPrimitivesDefaultedForNullValue(boolean)}
38+
* <p>The mapper can be configured to use the primitives default value when mapping null values
39+
* by setting the {@link #setPrimitivesDefaultedForNullValue 'primitivesDefaultedForNullValue'}
40+
* flag to 'true'.
4141
*
4242
* <p>To facilitate mapping between columns and fields that don't have matching names,
4343
* try using column aliases in the SQL statement like "select fname as first_name from customer".
@@ -59,19 +59,8 @@ public class ParameterizedBeanPropertyRowMapper<T> extends BeanPropertyRowMapper
5959
* @param mappedClass the class that each row should be mapped to
6060
*/
6161
public static <T> ParameterizedBeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
62-
return newInstance(mappedClass, false);
63-
}
64-
65-
/**
66-
* Static factory method to create a new ParameterizedBeanPropertyRowMapper
67-
* (with the mapped class specified only once).
68-
* @param mappedClass the class that each row should be mapped to
69-
* @param primitivesDefaultedForNullValue whether we're defaulting primitives when mapping a null value
70-
*/
71-
public static <T> ParameterizedBeanPropertyRowMapper<T> newInstance(Class<T> mappedClass, boolean primitivesDefaultedForNullValue) {
7262
ParameterizedBeanPropertyRowMapper<T> newInstance = new ParameterizedBeanPropertyRowMapper<T>();
7363
newInstance.setMappedClass(mappedClass);
74-
newInstance.setPrimitivesDefaultedForNullValue(primitivesDefaultedForNullValue);
7564
return newInstance;
7665
}
7766

0 commit comments

Comments
 (0)