Home > Allgemein > Fun with generics

Fun with generics

6. Juli 2010

We’ve been using the following construct for a long time in our util classes:

    public static <T> List<T> query(SimpleJdbcOperations jdbcOps, String query,
            Map<String, Object> paramMap) {
        return jdbcOps.query(query,
                new ParameterizedRowMapper<T>() {
                    @SuppressWarnings("unchecked")
                    public T mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                        return (T)rs.getObject(1);
                    }
                },
                paramMap);
    }

If you call this method with T=Long and the column to retrieve (in the database) is an int, Integer will be put to the list as (Long)Interger cast works. So you end up in a List filled with Integer. With that list, a list.remove(new Long(1)) does not remove.

KategorienAllgemein Tags:
Kommentare sind geschlossen