我收到以下错误:
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.
但是,当我将错误中列出的非常 select 语句复制并粘贴到 mysql shell 中时,我得到了结果,这是预期的,因为表track
中有 column 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());
}