Spring 3.1.2 RowMapper parameterization

13,047

Solution 1

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!

Solution 2

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.

Solution 3

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;
        }
    }

Solution 4

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>
Share:
13,047
Mighty
Author by

Mighty

Updated on June 04, 2022

Comments

  • Mighty
    Mighty almost 2 years

    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;
                  }
         }
    
  • Mighty
    Mighty over 11 years
    Actually, creating a private static should work as well:static.springsource.org/spring/docs/3.1.x/…
  • Evgeniy Dorofeev
    Evgeniy Dorofeev over 11 years
    Sure, just when I answered your code wasn't attached, will take a look now