Reputation: 71
I'm developing a web application using Spring 3.1.2, and need to create a custom row mapper. I have created a private static final class which implements RowMapper, but i'm getting the error message "The type RowMapper is not generic; it cannot be parameterized with arguments ".
All Spring related jars in my lib folder are of 3.1.2.RELEASE version. I have been unable to find anything of the sort elsewhere. Any ideas why this might be happening?
Thanks.
Here is the sample code:
public class OutPatient extends Patient{
@Pattern(regexp="[0-9]+", message="OPD No. should only contain digits.")
String opdNo;
public String getOpdNo() {
return opdNo;
}
public void setOpdNo(String opdNo) {
this.opdNo = opdNo;
}
}
DAO Class:
@Repository("dbHelper")
public class DBHelperImpl{
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public List<OutPatient> fetchOutPatients() {
String sql = "SELECT OPDNO as opdNo FROM `test`.`out_patient`";
@SuppressWarnings("unchecked") //Have to add this annotation to prevent warning
List<OutPatient> outPatients = jdbcTemplate.query(sql, new OutPatientRowMapper());
return outPatients;
}
private static final class OutPatientRowMapper implements RowMapper{ //Unable to add <OutPatient> generics here!
public OutPatient mapRow(ResultSet rs, int rowNum) throws SQLException {
OutPatient outPatient = new OutPatient();
outPatient.setOpdNo(rs.getString("opdNo"));
return outPatient;
}
}
Upvotes: 2
Views: 13962
Reputation: 61
The problem is that it cannot find the library to get RowMapper. The solution is to add the dependency.
These are the ones that you must add to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
Upvotes: -1
Reputation: 1050
None of the above answers worked for me. I had all the right Maven dependencies; they were just in the wrong order in the pom.xml file. After I changed the order to the order below, RowMapper was recognized as a parametrized class.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
</dependencies>
Upvotes: 1
Reputation: 956
I had this same kind of issue, Eclipse warning me about RowMapper not being generic.
So I wrote the import by hand:
import org.springframework.jdbc.core.RowMapper;
Which produced this error: The import org.springframework.jdbc.core.RowMapper collides with another import statement
So I looked at my other import statements and what do I find lurking in my Spring project:
import javax.swing.tree.RowMapper;
...I deleted that import and then everything worked as it should.
Upvotes: 8
Reputation: 89
you have to add these dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
Upvotes: 0
Reputation: 71
I found the solution to my problem after tinkering around a bit more. There were some old spring jars (2.0, I guess) in Tomcat's lib folder. That led to spring using an older version of RowMapper which didnot support parameterization.
Solution to this problem is either remove the older jars from the build path, or to change the order of the jars from the Order and Export tab and bring the latest jar to the top.
Thanks to all those who responded!
Upvotes: 0
Reputation: 136102
It should be something like this
List<Bank> list = t.query(sql, args, new RowMapper<Bank>() {
@Override
public Bank mapRow(ResultSet rs, int rowNum) throws SQLException {
Bank t = new Bank();
t.setName(rs.getString("name"));
return t;
}
});
This code also compiles without warnings
private static final class OutPatientRowMapper implements
RowMapper<OutPatient> {
public OutPatient mapRow(ResultSet rs, int rowNum) throws SQLException {
OutPatient outPatient = new OutPatient();
outPatient.setOpdNo(rs.getString("opdNo"));
return outPatient;
}
}
Upvotes: 3