12. Spring Boot Project(blogv1) 게시글 수정 오류

김미숙's avatar
Jul 15, 2025
12. Spring Boot Project(blogv1) 게시글 수정 오류

오류 발생

➡ 수정버튼을 누르면 오류 페이지가 뜬다
notion image
notion image
Hibernate: update board_tb set title = ?, content = ?,nickname = ? where id = ? 2025-03-20T09:50:16.033+09:00 WARN 8172 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 22018, SQLState: 22018 2025-03-20T09:50:16.033+09:00 ERROR 8172 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : Data conversion error converting "닉네임5"; SQL statement: update board_tb set title = ?, content = ?,nickname = ? where id = ? [22018-232] 2025-03-20T09:50:16.041+09:00 ERROR 8172 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: JDBC exception executing SQL [update board_tb set title = ?, content = ?,nickname = ? where id = ?] [Data conversion error converting "닉네임5"; SQL statement: update board_tb set title = ?, content = ?,nickname = ? where id = ? [22018-232]] [n/a]; SQL [n/a]] with root cause java.lang.NumberFormatException: For input string: "닉네임5"
⬇ 하단 코드를 보면 Query에 title, content, nickname, id 순서인데
setParameter는 id, title, content, nickname 순서로 되어있음
⚠ Mapping 오류 발생
public void update(int id, String title, String content, String nickname) { Query query = em.createNativeQuery("update board_tb set title = ?, content = ?,nickname = ? where id = ?"); query.setParameter(1, id); query.setParameter(2, title); query.setParameter(3, content); query.setParameter(4, nickname); query.executeUpdate(); }
 

🛠 해결

⬇ setParameter를 title, content, nickname, id 순서로 변경
public void update(int id, String title, String content, String nickname) { Query query = em.createNativeQuery("update board_tb set title = ?, content = ?,nickname = ? where id = ?"); query.setParameter(1, title); query.setParameter(2, content); query.setParameter(3, nickname); query.setParameter(4, id); query.executeUpdate(); }
notion image
notion image
notion image
notion image
H2Console 확인
notion image
notion image
Share article

parangdajavous