【问题标题】:Issue with SQL query not running via JdbcTemplateSQL 查询未通过 JdbcTemplate 运行的问题
【发布时间】: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


    【解决方案1】:

    好吧……好吧,我想通了……

    数据透视表列并不能很好地映射到我的行映射器。因此,我将行映射器更改为解决问题的以下内容:

    TranscriptStatusCounts tsc = new TranscriptStatusCounts();
        //'NEW','EXPORT_READY','PENDING_MATCH','MATCHED_ID','PROCESSED','REJECTED'
        tsc.setNewCount(rs.getLong(1));
        tsc.setExportReadyCount(rs.getLong(2));
        tsc.setPendingMatchCount(rs.getLong(3));
        tsc.setMatchedIdCount(rs.getLong(4));
        tsc.setProcessedCount(rs.getLong(5));
        tsc.setRejectedCount(rs.getLong(6));
        return tsc;
    

    忘了sql“Invalid Column Name”错误也可以参考resultSet中使用的名称来访问列。因为 PIVOT 查询对它们进行排序,所以我可以只使用列的编号来获取结果。

    【讨论】:

    • 这也解释了为什么当使用 jpa 作为本机查询尝试此操作时,我遇到了这样的问题。
    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 2019-02-09
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2021-11-12
    • 2018-03-16
    相关资源
    最近更新 更多