์ํฐํฐ(Entity)
JPA ์ํฐํฐ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ
์ด๋ธ๊ณผ ๋งคํ๋๋ ์์์ฑ ๊ฐ์ฒด
โข
์ฃผ์ ์ด๋
ธํ
์ด์
โข
์ํฐํฐ ์์ ์ฝ๋
์ฃผ์ ์ด๋
ธํ
์ด์
์ด๋
ธํ
์ด์
| ์ค๋ช
|
@Entity | ํด๋น ํด๋์ค๋ฅผ JPA ์ํฐํฐ๋ก ์ง์ |
@Table(name="table_name") | ์ํฐํฐ๊ฐ ๋งคํ๋ ํ
์ด๋ธ ์ง์ |
@Id | ๊ธฐ๋ณธ ํค(PK) ํ๋ ์ง์ |
@GeneratedValue
(strategy=GenerationType.IDENTITY) | ์๋ ์ฆ๊ฐํ๋ PK ์ค์ |
@Column
(name="column_name", nullable=false, length=100) | ํน์ ์ปฌ๋ผ ๋งคํ ๋ฐ ์์ฑ ์ค์ |
@ManyToOne | ๋ค๋์ผ(N:1) ๊ด๊ณ ์ค์ |
@OneToMany
(mappedBy="board") | ์ผ๋๋ค(1:N) ๊ด๊ณ ์ค์ |
@JoinColumn
(name="foreign_key") | ์ธ๋ ํค(FK) ๋งคํ |
@Enumerated
(EnumType.STRING) | ์ด๊ฑฐํ(Enum) ํ์
๋งคํ |
@Temporal
(TemporalType.TIMESTAMP) | ๋ ์ง ๋ฐ ์๊ฐ ํ์
๋งคํ |
@CreatedDate | ์์ฑ ์๊ฐ ์๋ ์ ์ฅ(Auditing ๊ธฐ๋ฅ ํ์ฉ) |
@LastModifiedDate | ์์ ์๊ฐ ์๋ ์ ์ฅ(Auditing ๊ธฐ๋ฅ ํ์ฉ) |
@Version | ๋๊ด์ ๋ฝ(Optimistic Lock) ์ ์ฉ |
@Transient | ์์์ฑ ๊ด๋ฆฌ ์ ์ธ(์ปฌ๋ผ์ผ๋ก ๋งคํ๋์ง ์์) |
์ํฐํฐ ์์ ์ฝ๋
Users ์ํฐํฐ
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import java.time.LocalDateTime;
import java.util.List;
@Entity
@Table(name = "users")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 50)
private String username;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Role role;
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Boards> boards;
public enum Role {
USER, ADMIN
}
}
Java
๋ณต์ฌ
Boards ์ํฐํฐ
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import java.time.LocalDateTime;
@Entity
@Table(name = "boards")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Boards {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100)
private String title;
@Column(nullable = false, columnDefinition = "TEXT")
private String content;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private Users user;
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
Java
๋ณต์ฌ
์ฃผ์ ํน์ง
์ด๋
ธํ
์ด์
| ์ค๋ช
|
@Entity | ์ํฐํฐ ํด๋์ค๋ก ์ง์ |
@Table(name="ํ
์ด๋ธ๋ช
") | ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ
์ด๋ธ๊ณผ ๋งคํ |
@Id, @GeneratedValue
(strategy = GenerationType.IDENTITY) | PK์ ์๋ ์ฆ๊ฐ ์ค์ |
@Column
(nullable = false, unique = true, length = 50) | ์ปฌ๋ผ ์์ฑ ์ง์ |
@Enumerated(EnumType.STRING) | ์ด๊ฑฐํ ๊ฐ ์ ์ฅ |
@CreatedDate, @LastModifiedDate | ์์ฑ ๋ฐ ์์ ์๊ฐ ์๋ ์ ์ฅ |
@ManyToOne, @OneToMany | ๊ด๊ณ ์ค์ ๋ฐ ๋งคํ |




