hibernate(reply) 2022/04/12

무간·2022년 4월 12일

파일명 BoardReplyEntity.java

package com.example.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.CreationTimestamp;
import org.springframework.format.annotation.DateTimeFormat;

import lombok.Data;
import lombok.ToString;

@Entity
@Data
@Table(name = "BOARD10_REPLY") // 테이블명
@SequenceGenerator(name = "SEQ1", 
        sequenceName = "SEQ_BOARD10_REPLY_RNO", 
        allocationSize =1, 
        initialValue = 1) // 생성항 시퀀스
public class BoardReplyEntity {

    @Id
    @Column(name = "RNO") // 기본키, 테이블명 RNO, 시퀀스 사용
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ1") // 시퀀스 적용
    private long no;
    
    @Column(name = "RCONTENT", length = 300) // VARCHAR2(300)
    private String content;

    @Column(name = "RWRITER", length = 50) // VARCHAR2(50)
    private String writer;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    @CreationTimestamp //CURRENT_DATE
    @Column(name = "BREGDATE")
    private Date regdate;

    @ToString.Exclude
    @ManyToOne // 외래키 BOARD10 테이블의 기본키 컬럼만 DB에 저장
    @JoinColumn(name = "BOARD")
    private BoardEntity board;

}

파일명 BoardEntity.java

일부

외래키 설정시 연결할때 사용
@OneToMany(mappedBy = "board")
private List<BoardReplyEntity> replyList = new ArrayList<>();

파일명 BoardReplyRepository.java

package com.example.repository;

import java.util.List;

import com.example.entity.BoardReplyEntity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BoardReplyRepository extends JpaRepository<BoardReplyEntity, Long> {

    // List<BoardReplyEntity> replyList
    // 원본 글번호가 일치하는 댓글 개수
    List<BoardReplyEntity> findByBoard_NoOrderByNoDesc(long bno);
    
}

파일명 BoardController.java

일부(댓글을 board에서 사용하기 때문)

@PostMapping(value = "/insertreply")
    public String insertReply(        
        @ModelAttribute BoardReplyEntity reply
    ){

        System.out.println("==========reply===========");
        System.out.println(reply.toString());
        
        brRepository.save(reply);
        return "redirect:/board/selectone?no=" + reply.getBoard().getNo();
    } 

파일명 board_selectone.html

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>게시판 상세</title>
</head>

<body style="padding: 10px;">
    <h3>게시판 상세</h3>

    <hr />
    
    <div style="padding:20px">

        
        <hr />

        <label style="width:75px; height: 30px; display:inline-block;">글번호 :</label>
        <P style="display:inline-block" th:text="${board.no}"></P><br />

        <label style="width:75px; height: 30px; display:inline-block;">제목 :</label>
        <P style="display:inline-block" th:text="${board.title}"></P><br />

        <label style="width:75px; height: 30px; display:inline-block;">작성자 :</label>
        <P style="display:inline-block" th:text="${board.writer}"></P><br />

        <label style="width:75px; height: 30px; display:inline-block;">내용 :</label>
        <P style="display:inline-block" th:text="${board.content}"></P><br />

        <label style="width:75px; height: 30px; display:inline-block;">조회수 :</label>
        <P style="display:inline-block" th:text="${board.hit}"></P><br />

        <label style="width:75px; height: 30px; display:inline-block;">등록일 :</label>
        <P style="display:inline-block" th:text="${board.regdate}"></P><br />
       
        <hr />

        <a th:href="@{/board/selectlist}"><button>목록으로</button></a>
        <a th:href="@{/board/selectpre(no=${board.no})}"><button>이전글</button></a>
        <a th:href="@{/board/selectnext(no=${board.no})}"><button>다음글</button></a>        
        <button>수정</button>
        <button>삭제</button>
        
        <hr />
        <form th:action="@{/board/insertreply(bno=${board.no})}" method = "post" >
            <input type="hidden" name = "board.no" th:value="${board.no}">
            <textarea rows="6" name="content" placeholder="답글작성"></textarea><br />
            <input type="text" name="writer" placeholder="작성자" /><br />
            <input type="submit" value="답글작성" /><br />
        </form>
        
        <hr />
        <h3>답글목록</h3>
        <table border="1">
            <tr>
        		<th>답글번호</th>        		
        		<th>작성자</th>
        		<th>내용</th>        		
        		<th>등록일</th>
        	</tr>
            <tr th:each="rep : ${board.replyList}">
                <td th:text="${rep.no}"></td>                
                <td th:text="${rep.writer}"></td>
                <td th:text="${rep.content}"></td>                
                <td th:text="${rep.regdate}"></td>
            </tr>
        </table>
        
        <hr />
        <table border="1">
            <tr>
        		<th>답글번호</th>        		
        		<th>작성자</th>
        		<th>내용</th>        		
        		<th>등록일</th>
        	</tr>
            <tr th:each="rep : ${repList}">
                <td th:text="${rep.no}"></td>                
                <td th:text="${rep.writer}"></td>
                <td th:text="${rep.content}"></td>                
                <td th:text="${rep.regdate}"></td>
            </tr>
        </table>

    </div>
</body>
</html>
profile
당신을 한 줄로 소개해보세요

10개의 댓글

comment-user-thumbnail
2023년 3월 13일

The idea of KwickMetrics was borne by learning about the difficulties and complexities that Amazon sellers have to face from talking to our sister company currently engaged in selling products on Amazon.
https://www.kwickmetrics.com/

답글 달기
comment-user-thumbnail
2023년 3월 17일

This website is just provide lyrics of the songs. We don't copy paste lyrics. We write all lyrics by own. We don't provide any download option from our website. This website is a lyrical website only.
https://www.thelyricalworld.com/

답글 달기
comment-user-thumbnail
2023년 4월 6일

You will love visiting or staying in Chicago as it boasts annual festivals and hundreds of parks, gardens, and other recreational areas.
https://usmaptimezones.com/

답글 달기
comment-user-thumbnail
2024년 12월 12일

this is a very useful blog i have read in my life. please post more blogs like this
https://himalayanoutback.com/

답글 달기
comment-user-thumbnail
2025년 4월 16일

Add a touch of effortless style and versatility to your look with this classic Bandana Headband. Crafted from soft, breathable fabric, it combines the iconic paisley bandana print with a comfortable, secure fit. Whether you're taming your hair for a workout, adding flair to your festival outfit, or just keeping it casual, this headband is your go-to accessory.
https://aryanarts33.com/collections/bandana

답글 달기
comment-user-thumbnail
2025년 4월 17일

I am professional Escort working independently in Gurgaon. I am quite skilled and knowledgeable in this industry, and I enjoy what I do. I am very stylish, daring, and attractive, and I have well-maintained, toned body parts.
To meet me click on the link below:
https://www.ctgal.com/call-girls/gurgaon/

답글 달기
comment-user-thumbnail
2025년 4월 19일

Sarong wrap beach cover up ✓ Buy 100% organic cotton beach wrap plus-size sarongs for men and women. These eco-friendly, soft, and breathable sarongs are perfect for lounging by the beach, pool, or as a stylish cover-up.
https://aryanarts33.com/collections/sarongs

답글 달기