Entity Class

Entity Class ๋ database table ์ mapping ๋ Java Class ๋ฅผ ์๋ฏธํ๋ค. Entity Class ๋ ORM ๊ธฐ์ ์ ์ฌ์ฉํ์ฌ OOP ์ DB ๊ฐ์ ์ฐ๊ฒฐ์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ค.
Entity Class ๋ก ์ฌ์ฉํ๊ธฐ ์ํ์ฌ๋ ๋ค์ ์ต์ํ์ ์กฐ๊ฑด์ด ํ์ํ๋ค.
@Entityannotated ๋์ด์ผ ํ๋ค.public๋๋protected์ธ default constructor (no-arguments) ๋ ๋ฐ๋์ ์กด์ฌํด์ผ ํ๋ค.
Java Annotation
๋ค์์ Java Annotation ์ ์ฌ์ฉํ์ฌ Java class ์ Database ๋ฅผ mapping ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
Step 1: Map class to database table
๋จผ์ class ๋ฅผ database ์ table ๊ณผ mapping ์์ผ์ฃผ๋ ์์ ์ด ํ์ํ๋ค.
@Entity
@Table(name="student")
public class Student {
...
}์ฐ์ @Entity annotation ์ ์ฌ์ฉํ์ฌ Student class ๋ฅผ Entity Class ๋ก ์ฌ์ฉํ๊ณ , @Table annotation ์ ์ฌ์ฉํ์ฌ explicit ํ๊ฒ class ์ database ์ table ์ mapping ํ๋ค.
Step 2: Map fields to database columns
๊ทธ ๋ค์์ class ์ ๊ฐ๊ฐ์ field ๋ฅผ, database table ์ ๊ฐ๊ฐ์ column ์ผ๋ก mapping ์์ผ์ฃผ๋ ์์ ์ด๋ค.
@Entity
@Table(name="student")
public class Student {
@Id
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
...
}@Column annotation ์ ์ฌ์ฉํ์ฌ explicit ํ๊ฒ field ์ column ์ mapping ์์ผ์ค ๋ชจ์ต์ด๋ค.
Implicit or Explicit
ํ์ง๋ง ์ด๋ ๊ฒ @Table, @Column annotation ์ ์ฌ์ฉํ์ฌ explicit ํ๊ฒ mapping ํ๋ ๊ฒ์ ์ถ์ฒ๋์ง ์๋๋ค. ์ฐ์ , implicit mapping ์ ํตํ์ฌ class name ์ table name ์ผ๋ก, field name ์ column name ์ผ๋ก ์๋ mapping ํ๋ ๊ท์น์ด ์ถฉ๋ถํ ๊ฐ๋ ฅํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋ํ, ์ฝ๋์ ๊ฐ๋ ์ฑ ์ธก๋ฉด์์๋ ํจ์ฌ ๊น๋ํด์ง ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฉฐ, Database ์ ์ค๊ณ๊ฐ ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ explicit ํ๊ฒ ์ค์ ๋ mapping ์ ๋ณด๋ ์์ ํด์ผ ํ ๋ถ๋ถ์ด ๋ ๋ง์์ง ์ ์๋ค. ์๋ฅผ ๋ค์ด implicit mapping ์์๋ class ๋ฐ field ์ ์ด๋ฆ๋ง ์์ ํ๋ ๊ฒ์ผ๋ก ์ ์ง๋ณด์๋ฅผ ์๋ฃํ ์ ์๋ค.
Primary Key
Primary Key ๋ RDB ์์ table ์ ๊ฐ row ๋ฅผ uniquely ํ๊ฒ ์๋ณํ๊ธฐ ์ํ์ฌ ์ฌ์ฉ๋๋ key ์ด๋ค. Primary ๋ ๋ค์ ํน์ง์ ๊ฐ์ง๋ค.
- ์ ์ผ์ฑ: key ๊ฐ์ table ๋ด์์ ์ค๋ณต๋ ์ ์๋ค.
- NULL ๊ฐ ๋ถํ: key ๊ฐ์ด ๋ฐ๋์ ์กด์ฌํด์ผ ํ๋ฉฐ, NULL ๊ฐ์ ๊ฐ์ง ์ ์๋ค.
- ๋จ์ผ์ฑ: ํ๋์ table ์๋ ํ๋์ primary key ๋ง ์ ์ํ ์ ์๋ค.
MySQL - Auto Increment
CREATE TABLE student (
id int NOT NULL AUTO_INCREMENT,
first_name varchar(45) DEFAULT NULL,
last_name varchar(45) DEFAULT NULL,
email varchar(45) DEFAULT NULL,
PRIMARY KEY (id)
)SQL ์์์ AUTO_INCREMENT ๋ฅผ ์ฌ์ฉํ์ฌ id ๋ฅผ 1์ฉ ์ฆ๊ฐ์ํด์ผ๋ก์จ uniqueness ๋ฅผ ์ ์งํ ์ ์๋ค. ๋ํ ๋ง์ง๋ง์ PRIMARY KEY (id) ๋ก column name ์ ์ง์ ํ์ฌ Primary Key ๋ก ์ค์ ํ๋ค.
JPA Identity - Primary Key
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
...
}JPA ์์๋ ํน์ field ์ @Id annotation ์ ์ถ๊ฐํด์ค์ผ๋ก์จ Primary Key ๋ฅผ ์ค์ ํ๋ค. ๋ํ @GeneratedValue() ๋ฅผ ์ง์ ํ์ฌ ์ง์ ๊ฐ์ ์ค์ ํ๋ ๊ฒ์ด ์๋, Database ๊ฐ ์๋์ผ๋ก ๊ด๋ฆฌํ๋ Primary Key ์ ์์ฑ ๋ฐฉ๋ฒ์ ์ค์ ํ ์ ์๋ค. ID Generation Strategies ๋ ๋ค์ ๊ฒ๋ค์ด ์๋ค.
JPA ID Generation Strategies
-
GenerationType.AUTO- JPA๊ฐ Database Dialect ์ ๋ฐ๋ผ ์ ์ ํ ID ์์ฑ ์ ๋ต์ ์๋์ผ๋ก ์ ํ
-
GenerationType.IDENTITY- Database ์ย
AUTO_INCREMENTย ๊ธฐ๋ฅ์ ์ฌ์ฉํด ID๋ฅผ ์์ฑ
- Database ์ย
-
GenerationType.SEQUENCE- Database ์ Sequence(DB ์์ ์์ฐจ์ ์ผ๋ก ์ฆ๊ฐํ๊ฑฐ๋ ๊ฐ์ํ๋ ์ซ์ ๊ฐ์ ์๋์ ์์ฑํด์ฃผ๋ object) ๋ฅผ ์ฌ์ฉํด ๊ณ ์ ํ ID ๋ฅผ ์์ฑ
@SequenceGenerator๋ฅผ ํตํด Sequence ์ค์ ๊ฐ๋ฅ
-
GenerationType.TABLE- ๋ณ๋์ Table ์ ๋ง๋ค์ด ID๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ, Sequence ๋ฅผ ํ๋ด๋ด๋ ๋ฐฉ์
- ๋ชจ๋ Database ์์ ์ฌ์ฉ ๊ฐ๋ฅํ์ง๋ง ์ฑ๋ฅ์ด ๋ฎ์ ์ ์ฌ์ฉ๋์ง ์์
-
GenerationType.UUID- ์ ์ธ๊ณ์ ์ผ๋ก ๊ณ ์ ํ UUID๋ฅผ ์์ฑํ์ฌ ID๋ก ์ฌ์ฉ, 128-bit ์ด๊ธฐ ๋๋ฌธ์ ๋คํธ์ํฌ ์ ์ฒด์์๋ ๊ณ ์ ํจ
- ๋ถ์ฐ ์์คํ ์์ ์ ์ฉํ์ง๋ง indexing ํจ์จ์ด ๋จ์ด์ง๊ณ ์ ์ฅ ๊ณต๊ฐ์ ๋ง์ด ์ฐจ์งํจ
์ด๋ค๋ฟ๋ง์ด ์๋๋ผ ์ง์ generation stategy ๋ฅผ ๊ตฌ์ฑํ ์๋ ์๋ค. org.hibernate.id.IdentifierGenerator Interface ์ public Serializable generate() method ๋ฅผ overriding ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ง์ ๊ตฌ์ฑํ ์ ์๋ค.