【发布时间】:2015-06-01 06:59:00
【问题描述】:
我遇到了一个奇怪的问题,即使用 Oracle Pivot 语法的 sql 查询。我可以毫无问题地在 SqlDeveloper 中运行查询;但是,使用 RowMapper 通过 JdbcTemplate 运行它会给出关于无效列名的奇怪错误。
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar ... nested exception is java.sql.SQLException: Invalid column name
SQL 语句:
select * from (
Select stat.SWRHXML_HXPS_CODE
FROM Swbhxml xml
LEFT JOIN Swrhxml stat ON stat.swrhxml_trans_id = xml.SWBHXML_TRANS_ID
WHERE stat.SWRHXML_ACTIVITY_DATE = (
SELECT MAX(st.SWRHXML_ACTIVITY_DATE)
FROM swrhxml st
WHERE stat.SWRHXML_TRANS_ID = st.SWRHXML_TRANS_ID)
) pivot (count(SWRHXML_HXPS_CODE)
for SWRHXML_HXPS_CODE in
('NEW','EXPORT_READY','PENDING_MATCH','MATCHED_ID','PROCESSED','REJECTED'));
行映射器:
public class TranscriptStatusCountRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
TranscriptStatusCounts tsc = new TranscriptStatusCounts();
tsc.setNewCount(rs.getLong("NEW_RECORDS"));
tsc.setExportReadyCount(rs.getLong("EXPORT_READY"));
tsc.setPendingMatchCount(rs.getLong("PENDING_MATCH"));
tsc.setMatchedIdCount(rs.getLong("MATCHED_ID"));
tsc.setProcessedCount(rs.getLong("PROCESSED"));
tsc.setRejectedCount(rs.getLong("REJECTED"));
return tsc;
}
}
DAO 调用类:
@Repository("transcriptCountDao")
public class TranscriptCountDaoImpl extends BaseDaoImpl implements TranscriptCountDao {
private static final Logger logger = Logger.getLogger(TranscriptCountDaoImpl.class);
@Override
public TranscriptStatusCounts findTranscriptStatusCount() {
logger.debug("Getting counts of Transcripts status in system");
String sql = "...sql posted above..."
TranscriptStatusCounts tsc =
(TranscriptStatusCounts) getJdbcTemplate().queryForObject(sql, new TranscriptStatusCountRowMapper());
return tsc;
}
}
【问题讨论】:
标签: java sql oracle11g pivot jdbctemplate