โ๏ธ ๋ฌธ์ ์ํฉ
Hibernate์์ @Version ํ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ํด๋ ํ๋๊ฐ null ์ด๋ฉด ๋ค์ ์ปค๋ฐ ์์ ์ NPE๊ฐ ๋ฐ์ํ ์ ์๋ค.
โ๏ธ ์์ธ
Hibernate๋ ๋ด๋ถ์ ์ผ๋ก version + 1์ ์ํํ๊ธฐ ์ํด ํ์ฌ ๋ฒ์ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ฐ,
@Version ํ๋๊ฐ null ์ด๋ฉด ์ด๋ฅผ ์ํํ ์ ์์ด ์์ธ๊ฐ ๋ฐ์ํ๋ค.
โ๏ธ ํด๊ฒฐ๋ฐฉ๋ฒ
์์ฑ์ version ๊ฐ์ ์ด๊ธฐํํด์ค๋ค.
...
@Version
private Integer version;
private Limited(UUID limitedProductId, LocalDateTime startDate,
LocalDateTime endDate, LimitedStatus status) {
LimitedDateValidator.validateDate(startDate, endDate);
this.limitedProductId = limitedProductId;
this.startDate = startDate;
this.endDate = endDate;
this.status = status;
this.version = 0;
}
...
+ ํน์ ๊ธฐ์กด ๋ฐ์ดํฐ๊ฐ ๋จ์ ์๋ค๋ฉด UPDATE SET ์ ์ด์ฉํด version์ ๊ฐ์ ๋ฃ์ด์ฃผ๊ฑฐ๋, ๋ฐ์ดํฐ๋ค์ ์ญ์ ํ๋ค.
โจ ๊ฒฐ๋ก
๋ฐ๋ผ์ ๋๊ด์ ๋ฝ์ ์ฌ์ฉํ ๋ ๋ฒ์ ํ๋๋ฅผ ํญ์ ์ ๊ฒฝ์จ์ ๊ด๋ฆฌํด์ผ ํ๋ค.
๋ํ ์ด๊ธฐํ๋์ง ์์ ์ํ์์ ์ ์ฅ๋๋ ์ํฐํฐ๋ ์ปค๋ฐ ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์์์ ์์ง ๋ง์์ผ ํ๋ค.