Jpa soft delete๊ตฌํ˜„

greenTeaยท2023๋…„ 6์›” 14์ผ
0

๐Ÿ˜€sql์„ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋ฉด ๊ธฐ์กด์— ์กด์žฌํ•˜๋˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋ฏธ ์ง€์›Œ๋ฒ„๋ฆฐ ๋ฐ์ดํ„ฐ๋ผ๋ฉด ์ฐพ์„ ์ˆ˜ ์—†๋Š”๋ฐ์š” soft delete ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๊ธฐ๋ณธ Entity

@Entity
public class Member {

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private int delete;
    
}

๐Ÿง์œ„์™€ ๊ฐ™์€ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด soft delete๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


soft delete ์ ์šฉ

@SQLDelete(sql = "UPDATE member SET delete = 1 WHERE id = ?")
@Where(clause = "delete = 0")
@Entity
public class Member {

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private int delete = 0;
    
}

๐Ÿฅณ@SQLDelete์˜ ๊ฒฝ์šฐ delete์‹œ ์ž๋™์œผ๋กœ ์œ„ ์ฟผ๋ฆฌ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋‚˜๊ฐ€๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
@Where์˜ ๊ฒฝ์šฐ member entity๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ sql์— "delete = 0"์ด๋ผ๋Š” sql ์ฟผ๋ฆฌ๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.

profile
greenTea์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€