์์ํ๋ฉฐ
์ฐ๋ฆฌ๋ Entity๋ฅผ ๋ง๋ค ๋ ์ฐ๋ฆฌ๋ PK (Primary Key)๋ฅผ ์ง์ ํ๋ค.
๊ทธ์ค์์๋ Primary Key๋ฅผ ์ ํํ ๋, ์ ๋ต์ ์ผ๋ก ์๊ฐํ์ ๋ ์์ฐจ ์ฆ๊ฐ ๊ฐ์ ํฌ๊ธฐํ ์๊ฐ ์๋ค.
๊ณผ์ฐ ๊ทธ๋ผ JPA์์๋ ์ด๋ฐ ๋์ฒด ํค๋ฅผ ์ด๋ป๊ฒ ์์ฑํ๊ณ , ์ด๋ค์์ผ๋ก ๋์ํ๋์ง์ ๋ํด ์์๋ณด๋๋ก ํ์.
GeneratedValue๋?
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
@Table(name = "coupon")
@Entity
@Getter
@Setter
public class Coupon implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "coupon_id", insertable = false, nullable = false)
private Long couponId;
@Column(name = "coupon", nullable = false, unique = true)
private String coupon;
@Column(name = "coupon_status", nullable = false)
private CouponStatus status;
@Column(name = "reg_timestamp", nullable = false)
private LocalDateTime regTimestamp;
@Column(name = "expired_timestamp", nullable = false)
private LocalDateTime expiredTimestamp;
}
JPA๋ ์ด๋ฌํ ๊ธฐ๋ณธํค๋ฅผ ์๋์์ฑ ํ๋ ๋ถ๋ถ์ ๋ํด '@Id'์ '@GeneratedValue' ์ด๋ ธํ ์ด์ ์ ํตํด ์ ๊ณต์ ํ๋ค.
์ฌ๊ธฐ์ ์๋ ์์ฐ ์ ๋ต์ 4๊ฐ์ง๋ก ์ ๊ณต ํ๊ฒ ๋๋ค.
๋ชจ๋ | ์ค๋ช |
Auto (Default) | JPA๊ฐ ์์์ ํ๊ฒ ์ |
IDENTITY | ๊ธฐ๋ณธํค ์์ฑ์ DB์๊ฒ ์์ |
SEQUENCE | DB์์ `SEQUENCE`๋ฅผ ์ง์ํ๋ค๋ฉด, ์ฌ์ฉ ์๋๋ผ๋ฉด Table Mode |
TABLE | DB์ ํค์ฉ ํ ์ด๋ธ์ด ์๊น (hibernate_sequence) |
GeneratedType.AUTO ๊ฐ ๋์ํ๋ ๋ฐฉ์
AUTO (Default)๋ก ์ฌ์ฉ ํ๋ฉด ์ ์์๋ ๋๋ก ์์คํ ์ด ๋์ํ๊ฒ ๋๋ค.
1. UUID๋ผ๋ฉด, UUID๋ก ์ฒ๋ฆฌ ํ๋ค.
2. UUID๊ฐ ์๋๊ณ Number Data Type์ด๋ผ๋ฉด `hibernate.id.new_generator_mapping`์ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ฒ ๋๋ค.
2-1. FALSE๋ผ๋ฉด, Native Genertaor๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค (MySQL์ด๋ผ๋ฉด `auto_increment`)
3. hibernate.id.new_generator_mapping ์ด TRUE ๋ผ๋ฉด `SequenceStyleGenetrator` ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค.
3-1. Sequence Call์ ํ๊ฒ ๋๊ณ , ID๋ฅผ ๋ฐ๋๋ค
3-2. ๋ง์ฝ ์ฌ์ฉํ๋ ค๊ณ ํ๋ DBMS๊ฐ Sequence API๋ฅผ ์ง์ํ์ง ์๋๋ค๋ฉด `GeneratedType.TABLE`๋ก ๋์ํ๊ฒ ๋๋ค.
GeneratedType.IDENTITY ๊ฐ ๋์ํ๋ ๋ฐฉ์
์ด ๋ฐฉ์์ ID ์์ฑ์ ๋ํด DB์ ๋ชจ๋ ๊ฒ์ ์์์ํค๋ ํํ์ด๋ค.
์ค์ ์ ๋ ฅ ๋จ๊ณ์์๋ NULL๋ก ์ค์ ๋์ด, DB์ ๋์ด๊ฐ๊ฒ ๋๋ค.
๊ฒฐ๋ก ์ ์ผ๋กID๋ฅผ JPA์์ ์ฑ์์ ๋ฃ์ง๋ ์๋๋ค.
ํ์ง๋ง, ์์์ฑ ์ปจํ ์คํธ์ ๋ฌด๊ฒฐ์ฑ์ ์ ์ง ํ๊ธฐ ์ํ์ฌ, ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๊ณ ๋ช๋ฒ ID๋ก ์ ์ฅ ํ์๋์ง ๋ฐ์์ค๊ฒ ๋๋ค.
GeneratedType.IDENTITY์์์ BULK INSERT?
์ต๊ทผ์ JPA (Hibernate ๊ตฌํ์ฒด)๋ฅผ BULK INSERT๋ฅผ ํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์์๋ค.
์ฑ๋ฅ์ด ๋๋ฌด ์๋์ค๊ธธ๋, SQL ์ฟผ๋ฆฌ๋ฅผ ์ฐ์ด๋ณด๋ ๊ทธ๋ฅ ๊ทธ๋๋ก ๋ ์๊ฐ๊ณ ์์๋ค.
ํ์ด๋ฒ๋ค์ดํธ ๊ณต์ ๋ฌธ์์์ ๊ทธ ์ด์ ๋ฅผ ์๊ฒ ๋์๋ค.
Hibernate ORM 5.4.17.Final User Guide
Fetching, essentially, is the process of grabbing data from the database and making it available to the application. Tuning how an application does fetching is one of the biggest factors in determining how an application will perform. Fetching too much dat
docs.jboss.org
Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.
'IDENTITY'๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ ํ ๊ฒฝ์ฐ JDBC ๋ ๋ฒจ์์ batch insert๋ฅผ ๋นํ์ฑํ ํ๋ค๋ ๊ฒ ์ด๋ค.
๊ทผ๋ฐ ์ ๊ทธ๋ฌ๋์ง์ ๋ํ ์ด์ผ๊ธฐ๋ ๊ณต์ ๋ฌธ์์ ์๊ธธ๋, ๋ค๋ฅธ ๋ธ๋ก๊ทธ์ ์๋ StackOverflow๊ธ์ ๊ฐ์ ธ์๋ค.
Hibernate disabled insert batching when using an identity identifier generator
The Hibernate documentation says: Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator. But all my entities have this configuration:...
stackoverflow.com
The only drawback is that we can’t know the newly assigned value prior to executing the INSERT statement. This restriction is hindering the “transactional write behind” flushing strategy adopted by Hibernate. For this reason, Hibernates disables the JDBC batch support for entities using the IDENTITY generator.
ํ์ค์์ฝ : hibernate์ 'transactional write behind'์ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์์ด์ ๋๋ค๊ฐ ์๋ฌด๋ฆฌ ์์ฒญํด๋ ๋ด๊ฐ ๊บผ๋ฒ๋ฆด๊บผ์ผ..
GeneratedType.IDENTITY ์ํ์์ Batch Insert๋?
SEQUENCE ์ฐ์ธ์ ์ฌ๋ฌ๋ถ(?) ์ด ๋ต์ด ๋ผ๊ณ ์ด์ผ๊ธฐ ํ๋ฉด ์ด๊ฑด ๋ฌด์ฑ ์ ํ ์ด์ผ๊ธฐ ์ด๊ณ , ๋ง์ฝ ๋ค์ ์ฐ๊ฒฐ๋๋ DB๊ฐ MYSQL์ด๋ผ๋ฉด ์๋์ ๊ธ์ ๊ผญ ๋ณด๊ธธ ๋ฐ๋๋ค.
HomoEfficio/dev-tips
๊ฐ๋ฐํ๋ค ๋ง์ฃผ์ณค๋ ์์ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ ์ ๋ฆฌ. Contribute to HomoEfficio/dev-tips development by creating an account on GitHub.
github.com
MYSQL์์๋ `SEQUENCE`API๊ฐ ์๋ค.. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Hibernate๋ Tableํํ๋ก ์งํํ๋ ค๊ณ ํ๋๋ฐ ๊ทธ๋ผ ์์ ๊ฐ์ ์ฌ์์ด ์ผ์ด๋๊ธฐ ์์ํ๋ค. (๋ง์๋ชป..)
์ํผ ๋ณธ๋ก ์ผ๋ก ๋์์์, ์๊น ๊ทธ Stackoverflow ๊ธ์ ์ด์ผ๊ธฐ๋ฅผ ๋ค์ํด๋ณด์.
Therefore, using IDENTITY is still the best choice on MySQL, and if you need batching for insert,
you can use jOOQ for that. Hibernate and jOOQ are a great combo.
๊ฒฐ๋ก ์ Hibernate๋ง ์ฌ์ฉํด์ ์ฒ๋ฆฌํ๊ธด ์ด๋ ค์ฐ๋ jooq๋ฅผ ๊ฐ์ด ์ฌ์ฉ ํด๋ผ. ๋์ ์ต๊ณ ์ ์กฐํฉ์ด๋ค. ๋ผ๊ณ ์ด์ผ๊ธฐ ํ๋ค.
๊ฒฐ๊ตญ ๋ ์ญ์ JOOQ๋ก ๊ตฌํ ํ๋๋ฐ, ์ง์ฆ๋๋์ผ์ด ๊ฝค ์์๋ค.
์ด ๋ถ๋ถ์ ๋ค๋ฅธ ์๋ฆฌ์ฆ์์ ๋ค๋ค๋ณด๊ธฐ๋ก ํ๋ค.
Part2์์๋?
์๋๋ ํ๋์ ๊ธ์์ ์ด์ผ๊ธฐ๋ฅผ ๋ค ํ์ด๋ณด๋ ค๊ณ ํ์์ผ๋, ์๊ฐ ๋ณด๋ค ๊ธ์ด ๊ธธ์ด์ง๊ธฐ๋ ํ์๋ค.
๊ทธ๋ฆฌ๊ณ `GeneratedType.SEQUENCE`์ ๋ํด์๋ ๊ฝค ๊ธด ์ด์ผ๊ธฐ๊ฐ ์์๋ฏ ํ์ฌ ๋๋ ์ ์ด์ผ๊ธฐ ํ๋ ค๊ณ ํ๋ค.
Part2์์๋ 'GeneratedType.SEQUENCE', 'GeneratedType.TABLE' ๊ทธ๋ฆฌ๊ณ ๊ฒฐ๋ก ์ ๋ํด ์ด์ผ๊ธฐ ํด๋ณด๋๋ก ํ๊ฒ ๋ค.
'๐ป ๋ฐฑ์๋ ๊ฐ๋ฐ > JPA - Hibernate' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
03. Soft Delete ๋ ํธํ๊ฒ ํ๊ธฐ (0) | 2021.07.15 |
---|---|
02. ๋น์ ์ @Transactional์ readOnly ์ต์ ์ ์ฐ๊ณ ์๋์? (3) | 2021.02.04 |
๋๊ธ