[JPA] Can not issue data manipulation statements with executeQuery().

2022. 12. 8. 23:34ใ†Backend/๐ŸŒฟ Spring

์—๋Ÿฌ ๋ฉ”์‹œ์ง€: Caused by: java.sql.SQLException: Can not issue data manipulation statements with executeQuery().


์›์ธ: @Modifying(clearAutomatically = true) ๋ฏธ์‚ฌ์šฉ


`update, delete, insert` ์ฟผ๋ฆฌ๋Š” ๋ฆฌํ„ด๊ฐ’์„ ๊ฐ–์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ. 
Jpa ์—์„œ `select Query` ๋ฅผ ๋ณด๋‚ผ ๋• `executeQuery()` ๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, `update, delete, insert` ๋“ฑ์˜ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋‚ผ ๋•, executeUpdate() ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. executeUpdate() ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ „์†กํ•ด์„œ ๋ณ€๊ฒฝ๋œ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.


JPA ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ์‹œ, @Modifying ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ’ ์‘๋‹ต์„ ๊ธฐ๋Œ€ํ•˜๋Š” executeQuery() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ `update, delete, insert` ํŠธ๋žœ์žญ์…˜์„ JPA ์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด executeUpdate() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก @Modifying ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.

 

์œ„์˜ ์ด๋ฏธ์ง€์—์„  @Modifying ์–ด๋…ธํ…Œ์ด์…˜์ด ์ฃผ์„์ฒ˜๋ฆฌ ๋๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ์ฃผ์„ ํ•ด์ œํ•˜๋ฉด executeSelect() ๋Œ€์‹  executeUpdate() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

 

 

Todo : JPA ๋‚ด๋ถ€์—์„œ executeSelect(), executeUpdate() ๋ฉ”์„œ๋“œ๊ฐ€ ์–ด๋–ค ์›๋ฆฌ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ ํ•„์š”.