我收到了以下错误信息:
I am receiving the following error:
HTTP Status 500 - Request processing failed; nested exception is
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar
[SELECT id, name FROM track WHERE category_id = 1 ORDER BY name]; nested exception is
java.sql.SQLException: Column 'category_id' not found.
但是,当我将错误提示中列出的选择语句复制并粘贴到mysql shell中时,我得到了预期结果,因为表 track
具有列 category_id
。
造成此错误的原因是什么?
这是用于 track
的表创建语句:
CREATE TABLE track (
id SERIAL
,name VARCHAR(50)
,category_id BIGINT UNSIGNED -- This references a serial (bigint unsigned)
,CONSTRAINT track_id_pk PRIMARY KEY (id)
,CONSTRAINT track_category_id_fk FOREIGN KEY
(category_id) REFERENCES category (id)
);
这里是我 dao 类中关于 track
表的部分代码:
private static final class TrackMapper implements RowMapper<Track> {
@Override
public Track mapRow(ResultSet resultSet, int rowNum) throws SQLException {
Track track = new Track();
track.setId(resultSet.getInt("id"));
track.setName(resultSet.getString("name"));
track.setCategoryId(resultSet.getInt("category_id"));
return track;
}
}
public List<Track> getTracks(int categoryId) {
String sql = "SELECT id, name FROM track WHERE category_id = " + categoryId + " ORDER BY name";
return jdbcTemplate.query(sql, new TrackMapper());
}
resultSet.getLong
而不是getInt
。 - Thomas