인프런 커뮤니티 질문&답변
상품 삭제, CRUD
작성
·
303
0
안녕하세요 현재 기능상 Create Update Read 밖에 없길래 Delete를 추가하려고 제가 한번 추가해봤는데요. 문제가 발생해서 질문 드립니다.
Controller는 이렇구요
@GetMapping("items/{itemId}/delete")
public String deleteItem(@PathVariable Long itemId) {
itemService.deleteItem(itemId);
return "redirect:/items";
}
Service 로직은 단순 Repository를 조회하여 쿼리를 날리는 로직입니다.
public void deleteItem(Long itemId) {
itemRepository.delete(itemId);
}
그리고 Repository는
public void delete(Long id) {
em.createQuery("delete from Item i where i.id = :id", Item.class)
.setParameter("id", id);
}
이렇습니다.
그리고 삭제 버튼을 추가하기 위해 itemList.html에
<a href="#" th:href="@{/items/{id}/delete (id=${item.id})}" class="btn btn-danger" role="button">삭제</a>
해당 코드를 추가하였습니다 그래서 순조롭게 Application Run을 하고 삭제버튼을 눌렀는데!
IllegalArgumentException : Update/delete queries cannot be typed
라는 Exception이 터지면서 HttpStatus 500이 터지네요 ㅠㅠ 해결방법이 뭘까요?
답변 2
0
0
안녕하세요. 김태민님, 공식 서포터즈 OMG입니다.
에러메시지 검색하는데 Okky에 올리신 글 보고 '살짝' 놀랐네요 ㅎㅎ
아래의 링크를 보시면 아시겠지만
em.createQuery("delete from Item i where i.id = :id", Item.class)
두번째 인자인 Item.class를 제거해주세요.
참고)
https://titanwolf.org/Network/Articles/Article?AID=8d1e27b8-9ceb-4873-97d0-69cec44c31a7
감사합니다.
itemService의 delete처리하는 메서드위에@Transactional 어노테이션을 추가하셨나요?
클래스 레벨에서 @Transactional readOnly=true로 되어있는지요?
확인 결과 적용되어있지 않아서 적용후 Re-run해서 테스트 해본결과 그대로입니다 ㅠㅠ
삭제 후 쿼리도 날라가지 않네요
2021-12-02 11:49:52.106 INFO 15050 --- [nio-8080-exec-6] p6spy : #1638413392106 | took 0ms | commit | connection 9| url jdbc:h2:tcp://localhost/~/jpashop
;
2021-12-02 11:49:52.122 INFO 15050 --- [nio-8080-exec-7] j.jpashop.controller.HomeController : home controller
삭제한 직후 찍은 LOG입니다
무엇이 문제일까요.. Mapping이 Get으로 되어있어서 그런건가요? @DeleteMapping으로 바꾸고 클래스레벨에 RequestMapping("/")을 추가해도 Get Not Support Exception이 뜨네요 ㅠㅠ
제 전체 소스코드 링크입니다 https://github.com/TeMlN/Real-JPA-EX1






답변 너무 감사드립니다 !! 하지만.. White Label 에러 페이지는 해결 되었지만 정상작동이 안되고 있습니다 ㅠㅠ 삭제를 해도 delete 쿼리가 날라가는것도 안보이고 form에도 그대로 유지되어있네요 ㅠㅠ
삭제 직후 redirect:/ 를 했을때 날라가는 쿼리입니다.
Hibernate:
select
item0_.item_id as item_id2_3_,
item0_.name as name3_3_,
item0_.price as price4_3_,
item0_.stock_quantity as stock_qu5_3_,
item0_.artist as artist6_3_,
item0_.etc as etc7_3_,
item0_.author as author8_3_,
item0_.isbn as isbn9_3_,
item0_.actor as actor10_3_,
item0_.director as directo11_3_,
item0_.dtype as dtype1_3_
from
item item0_
2021-12-02 10:27:11.237 INFO 14679 --- [nio-8080-exec-8] p6spy : #1638408431237 | took 0ms | statement | connection 18| url jdbc:h2:tcp://localhost/~/jpashop
select item0_.item_id as item_id2_3_, item0_.name as name3_3_, item0_.price as price4_3_, item0_.stock_quantity as stock_qu5_3_, item0_.artist as artist6_3_, item0_.etc as etc7_3_, item0_.author as author8_3_, item0_.isbn as isbn9_3_, item0_.actor as actor10_3_, item0_.director as directo11_3_, item0_.dtype as dtype1_3_ from item item0_
select item0_.item_id as item_id2_3_, item0_.name as name3_3_, item0_.price as price4_3_, item0_.stock_quantity as stock_qu5_3_, item0_.artist as artist6_3_, item0_.etc as etc7_3_, item0_.author as author8_3_, item0_.isbn as isbn9_3_, item0_.actor as actor10_3_, item0_.director as directo11_3_, item0_.dtype as dtype1_3_ from item item0_;
2021-12-02 10:27:11.238 INFO 14679 --- [nio-8080-exec-8] p6spy : #1638408431238 | took 0ms | commit | connection 18| url jdbc:h2:tcp://localhost/~/jpashop
;
2021-12-02 10:27:12.184 INFO 14679 --- [io-8080-exec-10] p6spy : #1638408432184 | took 0ms | commit | connection 19| url jdbc:h2:tcp://localhost/~/jpashop
;
2021-12-02 10:27:12.201 INFO 14679 --- [nio-8080-exec-1] j.jpashop.controller.HomeController : home controller
삭제를 하려면 삭제하려는 id에 맞는 데이터들을 가지고 오기위해 select문이 나가는거 같기도 하지만 정작 중요한 delete쿼리가 날라가지를 않네요 ㅠㅠ.. 이유가 뭘까요? ItemRepository의 query가 잘못 된 걸까요? 혹시 CASCADE 로 연관된 Entity를 다 날려줘야 하는건가요? Abstract Entity라서 인가요?