λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

개발/Spring

JDBC Templete + PostgreSql ERROR (Can't infer the SQL type to use for an instance of java.util.ArrayList.)

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. 

Use setObject() with an explicit Types value to specify the type to use.

 

 

μ›ν•˜λ˜ μž‘μ—…

각각 κ²€μƒ‰ν–ˆμ„ μ‹œ Query의 κ°―μˆ˜κ°€ λ„ˆλ¬΄ λ§Žμ•„μ§€λŠ” 것을 μš°λ €ν•΄ listμ•ˆμ— idκ°€ μžˆλŠ” 경우 κ°€μ Έμ˜€λŠ” SQL을 짜렀고 ν–ˆλ‹€.
select * from public.game_id_name where app_id in (?)

 

였λ₯˜ λ°œμƒ μ½”λ“œ

ν•˜μ§€λ§Œ μœ„μ˜ μ—λŸ¬κ°€ λ°œμƒν–ˆκ³  postgresql의 경우 java list νƒ€μž…μœΌλ‘œ  μ‘°νšŒν•  수 μ—†λ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€.
public List<GameInfo> gamesDetailInfoFindById(List<Long> idList) {

    String sql = "select * from public.game_id_name where app_id in (?)";

    try {
        return jdbcTemplate.query(refinedSQL, gameRowMapper(), idList);
    } catch (IncorrectResultSizeDataAccessException e) {
        throw new CommonException(ResponseCode.INVALID_GAME_ID);
    }
}

 

ν•΄κ²° μ½”λ“œ

μ—¬λŸ¬ λ°©μ‹μœΌλ‘œ μ‹œλ„ν–ˆμœΌλ‚˜ 이해 κ°€λŠ₯ν•œ μ„ μ—μ„œ κ°€μž₯ κ°„νŽΈν•œ 방식은 "(1, 2, 3)" 이런 꼴둜 string을 λ§Œλ“€μ–΄ λŒ€μ²΄ν•˜λŠ” λ°©μ‹μ΄μ—ˆλ‹€.
    public List<GameInfo> gamesDetailInfoFindById(ArrayList<Long> idList) {

        String sql = "select * from public.game_id_name where app_id in (:ids)";

        String idListString = "";
        int i=0;
        for (Long id: idList){
            if (i < idList.size()-1){
                idListString = idListString + id + ",";
            }
            else {
                idListString = idListString + id;
            }
            i++;
        }
        String refinedSQL = sql.replace(":ids",idListString);

        try {
            return jdbcTemplate.query(refinedSQL, gameRowMapper());
        } catch (IncorrectResultSizeDataAccessException e) {
            throw new CommonException(ResponseCode.INVALID_GAME_ID);
        }
    }

 

 

μ™œ array ν˜•μ‹μ΄ 지원이 μ•ˆλ˜λŠ” 것인지 μ˜μ•„ν•˜λ‹€..

orm이 많이 μ“°μ΄λ‹€λ³΄λ‹ˆ κ·ΈλŸ°μ§€

κ½€ 자주 λ°œμƒν•  수 μžˆλŠ” 였λ₯˜μž„에도 κ΅¬κΈ€λ§μ‹œ λ§Žμ€ μžλ£Œκ°€ λ‚˜μ˜€μ§€ μ•Šμ•„ ν¬μŠ€νŒ… ν•΄λ³Έλ‹€!

 

 

 

 

λ°˜μ‘ν˜•