๐ก
๊ฒ์๊ธ ์ญ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
BoardRepository ์์ ๊ฒ์๊ธ ์ญ์ ์ฟผ๋ฆฌ๋ฅผ ๋ง๋ค์ด ๋ณด์.
JPA API , JPQL ๊ฐ์ฒด ์งํฅ ์ฟผ๋ฆฌ ์์ฑ (์ญ์ ๊ถํ)
package com.tenco.blog_v1.board;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@RequiredArgsConstructor
@Repository // IoC
public class BoardRepository {
private final EntityManager em;
/**
* ๊ฒ์๊ธ ์กฐํ ๋ฉ์๋
* @param id ์กฐํํ ๊ฒ์๊ธ ID
* @return ์กฐํ๋ Board ์ํฐํฐ, ์กด์ฌํ์ง ์์ผ๋ฉด null ๋ฐํ
*/
public Board findById(int id) {
return em.find(Board.class, id);
}
/**
* JPQL์ FETCH ์กฐ์ธ ์ฌ์ฉ - ์ฑ๋ฅ ์ต์ ํ
* ํ๋ฐฉ์ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ์ฆ, ์ง์ ์กฐ์ธํด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ ์ต๋๋ค.
* @param id
* @return
*/
public Board findByIdJoinUser(int id) {
// JPQL -> Fetch join ์ ์ฌ์ฉํด ๋ณด์.
String jpql = " SELECT b FROM Board b JOIN FETCH b.user WHERE b.id = :id ";
return em.createQuery(jpql, Board.class)
.setParameter("id", id)
.getSingleResult();
}
/**
* ๋ชจ๋ ๊ฒ์๊ธ ์กฐํ
* @return ๊ฒ์๊ธ ๋ฆฌ์คํธ
*/
public List<Board> findAll() {
TypedQuery<Board> jpql = em.createQuery(" SELECT b FROM Board b ORDER BY b.id DESC ", Board.class);
return jpql.getResultList();
}
// em.persist(board) -> ๋น์์ ์ํ์ธ ์ํฐํฐ๋ฅผ ์์์ํ๋ก ์ ํ
@Transactional
public Board save(Board board) {
em.persist(board);
return board;
}
/**
* ๊ฒ์๊ธ ์ญ์ ํ๊ธฐ
* @param id
*/
// DELETE JPA API ๋ฉ์๋๋ฅผ ํ์ฉ(์์์ฑ ์ปจํ
ํธ), JPQL --> QDSL .. namedQuery. ...
@Transactional // ํธ๋์ญ์
๋ด์์ ์คํ๋๋๋ก ๋ณด์ฅ
public void deleteById(int id) {
Query jpql = em.createQuery("DELETE FROM Board b WHERE b.id = :id");
jpql.setParameter("id", id);
jpql.executeUpdate();
}
/**
*
* JPA API ๋ง๋ค์ด ๋ณด์ธ์
*/
// DELETE JPA API ๋ฉ์๋๋ฅผ ํ์ฉ(์์์ฑ ์ปจํ
ํธ), JPQL --> QDSL .. namedQuery. ...
@Transactional // ํธ๋์ญ์
๋ด์์ ์คํ๋๋๋ก ๋ณด์ฅ
public void deleteByIdJPA(int id) {
}
}
BoardController ์์ ๊ฒ์๊ธ ์ญ์ ์์ฒญ์ ๋ง๋ค์ด ๋ณด์
// ์ฃผ์์ค๊ณ - http://localhost:8080/board/10/delete ( form ํ์ฉ์ด๊ธฐ ๋๋ฌธ์ delete ์ ์ธ)
// form ํํฌ์์๋ GET, POST ๋ฐฉ์๋ง ์ง์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
@PostMapping("/board/{id}/delete")
public String delete(@PathVariable(name = "id") Integer id) {
// ์ ํจ์ฑ, ์ธ์ฆ๊ฒ์ฌ
// ์ธ์
์์ ๋ก๊ทธ์ธ ์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ -> ์ธ์ฆ(๋ก๊ทธ์ธ ์ฌ๋ถ) , ์ธ๊ฐ(๊ถํ - ๋ด๊ธ?)
User sessionUser = (User) session.getAttribute("sessionUser");
if (sessionUser == null) {
return "redirect:/login-form";
}
// ๊ถํ ์ฒดํฌ
Board board = boardRepository.findById(id);
if(board == null) {
return "redirect:/error-404";
}
if( ! board.getUser().getId().equals(sessionUser.getId())) {
return "redirect:/error-403"; // ์ถํ ์์
}
// ๊ฒ์๊ธ ์ญ์
boardRepository.deleteById(id);
// boardNativeRepository.deleteById(id);
return "redirect:/";
}
detail.mustache
<!-- ์์ , ์ญ์ ๋ฒํผ -->
<div class="d-flex justify-content-end">
<a href="/board/{{board.id}}/update-form" class="btn btn-warning me-1">์์ </a>
<form action="/board/{{board.id}}/delete" method="post">
<button class="btn btn-danger">์ญ์ </button>
</form>
</div>
JPA API ๋ฉ์๋ ํ์ฉํด์ ๋ง๋ค์ด ๋ณด๊ธฐ!
/**
*
* JPA API ๋ง๋ค์ด ๋ณด์ธ์
*/
// DELETE JPA API ๋ฉ์๋๋ฅผ ํ์ฉ(์์์ฑ ์ปจํ
ํธ), JPQL --> QDSL .. namedQuery. ...
@Transactional // ํธ๋์ญ์
๋ด์์ ์คํ๋๋๋ก ๋ณด์ฅ
public void deleteByIdJPA(int id) {
em.remove(em.find(Board.class, id));
}
BoardController ์ฝ๋ ์์