๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

JPA

JPA Annotation

 

reference
์Šคํƒ€ํŠธ ์Šคํ”„๋ง ๋ถ€ํŠธ - ๊ตฌ๋ฉ๊ฐ€๊ฒŒ ์ฝ”๋”ฉ๋‹จ

www.datanucleus.org/products/accessplatform/jpa/annotations.html


code

๊ธฐ๋ณธ์ ์œผ๋กœ lombok ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import ๋ฌธ๋„ ๊ฐ™์ด ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ฒจ๋ถ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

package org.zerock.domain;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
@Table(name = "tbl_boards")
public class Board {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long bno;
  private String title;
  private String writer;
  private String content;

  @CreationTimestamp
  private Timestamp regdate;
  @UpdateTimestamp
  private Timestamp updatedate;

}

@Table

ํด๋ž˜์Šค๊ฐ€ ํ…Œ์ด๋ธ”์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ํด๋ž˜์Šค์˜ ์„ ์–ธ๋ถ€์— ์ž‘์„ฑํ•˜์—ฌ ํ…Œ์ด๋ธ”๋ช…์ด ์–ด๋–ป๊ฒŒ ์ง€์ •ํ• ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.

๋งŒ์ผ @Table์ด ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด, ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋œ๋‹ค.

attribute: name

ํด๋ž˜์Šค๋ช…๊ณผ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

attribute: catalog

ํ…Œ์ด๋ธ” ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

attribute: schema

ํ…Œ์ด๋ธ” ์Šคํ‚ค๋งˆ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

attribute: uniqueConstraints

์นผ๋Ÿผ๊ฐ’ ์œ ๋‹ˆํฌ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ค€๋‹ค.

attribute: indexes

์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

@Id

๊ฐ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋‹น column์— ์‹๋ณ„ํ‚ค (Primary Key - PK)๋ฅผ ์ง€์ •ํ•œ๋‹ค.

์ฃผ๋กœ @GeneratedValue ๋ผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ๊ฐ™์ด ์ด์šฉํ•ด์„œ ์‹๋ณ„ํ‚ค๋ฅผ ์–ด๋–ค ์ „๋žต์œผ๋กœ ์ƒ์„ฑํ•˜๋Š”์ง€๋ฅผ ๋ช…์‹œํ•œ๋‹ค.

 @GeneratedValue

strategy: AUTO

ํŠน์ • DB์— ๋งž๊ฒŒ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋ฐฉ์‹

strategy: IDENTITY

๊ธฐ๋ณธ ํ‚ค ์ƒ์„ฑ ๋ฐฉ์‹ ์ž์ฒด๋ฅผ DB์— ์œ„๋ฏธํ•˜๋Š” ๋ฐฉ์‹. DB์— ์˜์กด์ ์ธ ๋ฐฉ์‹.

MySQL ์—์„œ ์ฃผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

strategy: SEQUENCE

DB์˜ ์‹œํ€€์Šค๋ฅผ ์ด์š”ํ•˜์—ฌ ์‹๋ณ„ํ‚ค ์ƒ์„ฑ.

์˜ค๋ผํด์—์„œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

strategy: TABLE

๋ณ„๋„์˜ ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ๋Š” ์ฑ„๋ฒˆ ํ…Œ์ด๋ธ”(๋ฒˆํ˜ธ๋ฅผ ์ทจํ•  ๋ชฉ์ ์œผ๋กœ ๋งŒ๋“  ํ…Œ์ด๋ธ”)์„ ์ด์šฉํ•œ ๋ฐฉ์‹

@Column

DB์˜ ํ…Œ์ด๋ธ”์„ ๊ตฌ์„ฑํ•  ๋•Œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๊ฐ€ COLUMN์ด ๋œ๋‹ค.์•„๋ž˜์˜ ์„ค์ •์„ ํ†ตํ•ด COLUMN ๋ช…์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ์‚ฌ์ด์ฆˆ, ์ œ์•ฝ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

attribute: name

์นผ๋Ÿผ ์ด๋ฆ„

attribute: unique

์œ ๋‹ˆํฌ ์—ฌ๋ถ€, true/false

attribute: nullable

null ํ—ˆ์šฉ ์—ฌ๋ถ€ !!!!, true/false

attribute: insertable

insert ๊ฐ€๋Šฅ ์—ฌ๋ถ€, true/false

attribute: updatable

update ๊ฐ€๋Šฅ ์—ฌ๋ถ€, true/false

attribute: table

ํ…Œ์ด๋ธ” ์ด๋ฆ„

attribute: length

column ์‚ฌ์ด์ฆˆ

attribute: precision

์†Œ์ˆ˜ ์ •๋ฐ€๋„

attribute: scale

์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ์ˆ˜

'JPA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Entity Life Cycle  (0) 2021.01.21
Entity, Entity Manager  (0) 2021.01.21
Data source ์„ค์ •, JPA ๊ด€๋ จ ์„ค์ •  (0) 2021.01.21
JPA  (0) 2021.01.21