์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

STEP 02 - ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ

๋ฏธ๋กœ910 2024. 10. 8. 12:50
๐Ÿ’ก
๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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 ์ฝ”๋“œ ์ˆ˜์ •