유저 객체(entity) 만들기
package shop.mtcoding.blog.user;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import java.sql.Timestamp;
@NoArgsConstructor
@Data
@Table(name = "user_tb")
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String username;
// 해쉬로 바꾸면 문자의 개수가 늘어나니 숫자 제한을 12자로 하면 안된다.
private String password;
private String email;
@CreationTimestamp // insert 될 때 날짜를 자동으로 넣어준다.
private Timestamp createdAt;
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
}
보더 엔티티 수정
package shop.mtcoding.blog.board;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import shop.mtcoding.blog.user.User;
import java.sql.Timestamp;
@NoArgsConstructor
@Data
@Table(name = "board_tb")
@Entity
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String content;
// orm
// @JoinColumn(name = "user_id") // db에 만들어질 이름을 지정할 수 있음
@ManyToOne
private User user; // user_id 유저객체에 있는 pk를 잡아서 필드를 만들어준다.
@CreationTimestamp // insert 될 때 날짜를 자동으로 넣어준다.
private Timestamp createdAt;
public Board(String title, String content) {
this.title = title;
this.content = content;
}
public void update(BoardRequest.UpdateDTO requestDTO) {
this.title = requestDTO.getTitle();
this.content = requestDTO.getContent();
}
}
더미 데이터 수정
insert into user_tb(username, password, email, created_at) values('ssar', '1234', '[email protected]', now());
insert into user_tb(username, password, email, created_at) values('cos', '1234', '[email protected]', now());
insert into user_tb(username, password, email, created_at) values('love', '1234', '[email protected]', now());
insert into board_tb(title, content, user_id, created_at) values('제목1','내용1',1,now());
insert into board_tb(title, content, user_id, created_at) values('제목2','내용2',1,now());
insert into board_tb(title, content, user_id, created_at) values('제목3','내용3',2,now());
insert into board_tb(title, content, user_id, created_at) values('제목4','내용4',3,now());
@ManyToOne