簡體   English   中英

無法通過JdbcTemplate運行SQL查詢的問題

[英]Issue with SQL query not running via JdbcTemplate

我在使用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;
    }
}

好...我想通了...

數據透視表的列並不能很好地映射到我的行映射器。 因此,我將行映射器更改為以下解決問題的方法:

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查詢對它們進行排序,所以我可以僅使用列號來獲取結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM