diff --git a/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java b/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java index b8e40e17..ae774f30 100644 --- a/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java +++ b/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java @@ -8,7 +8,7 @@ import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; -import org.hibernate.annotations.SoftDelete; +import org.hibernate.annotations.*; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -20,7 +20,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor @SuperBuilder -@SoftDelete // boolean 타입의 deleted 필드가 추가 +@SoftDelete(columnName = "deleted") // boolean 타입의 deleted 필드가 추가 @Getter public abstract class BaseEntity { @@ -36,4 +36,7 @@ public abstract class BaseEntity { @Column(name = "updated_at") private LocalDateTime updatedAt; + @Column(name = "deleted", insertable = false, updatable = false) + private boolean deleted; + } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/category/entity/Category.java b/src/main/java/com/bbteam/budgetbuddies/domain/category/entity/Category.java index b25530f3..0d0c761a 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/category/entity/Category.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/category/entity/Category.java @@ -26,7 +26,7 @@ public class Category { @ColumnDefault("1") private Boolean isDefault; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/discountinfolike/entity/DiscountInfoLike.java b/src/main/java/com/bbteam/budgetbuddies/domain/discountinfolike/entity/DiscountInfoLike.java index bb3fda22..387e700a 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/discountinfolike/entity/DiscountInfoLike.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/discountinfolike/entity/DiscountInfoLike.java @@ -10,6 +10,8 @@ import jakarta.persistence.ManyToOne; import lombok.*; import lombok.experimental.SuperBuilder; +import org.hibernate.annotations.NotFound; +import org.hibernate.annotations.NotFoundAction; @Entity @Getter @@ -18,11 +20,13 @@ @SuperBuilder public class DiscountInfoLike extends BaseEntity { - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) + @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "user_id") private User user; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) + @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "discount_info_id") private DiscountInfo discountInfo; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/entity/Expense.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/entity/Expense.java index 0d94c760..814aad79 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/entity/Expense.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/entity/Expense.java @@ -10,6 +10,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; +import org.hibernate.annotations.Where; import java.time.LocalDateTime; @@ -28,11 +29,11 @@ public class Expense extends BaseEntity { private LocalDateTime expenseDate; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "category_id") private Category category; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/supportinfolike/entity/SupportInfoLike.java b/src/main/java/com/bbteam/budgetbuddies/domain/supportinfolike/entity/SupportInfoLike.java index 1480282f..78dc6607 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/supportinfolike/entity/SupportInfoLike.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/supportinfolike/entity/SupportInfoLike.java @@ -9,6 +9,9 @@ import jakarta.persistence.ManyToOne; import lombok.*; import lombok.experimental.SuperBuilder; +import org.hibernate.annotations.NotFound; +import org.hibernate.annotations.NotFoundAction; +import org.hibernate.annotations.Where; @Entity @Getter @@ -17,11 +20,13 @@ @SuperBuilder public class SupportInfoLike extends BaseEntity { - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) + @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "user_id") private User user; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) + @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "support_info_id") private SupportInfo supportInfo; diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/discountinfo/repository/DiscountInfoRepositoryTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/discountinfo/repository/DiscountInfoRepositoryTest.java index 84cf5776..31826927 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/discountinfo/repository/DiscountInfoRepositoryTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/discountinfo/repository/DiscountInfoRepositoryTest.java @@ -25,6 +25,37 @@ class DiscountInfoRepositoryTest { @Autowired private DiscountInfoRepository discountInfoRepository; + @Test + @DisplayName("@SoftDelete 테스트") + void deletedTest() { + // given + DiscountInfo discount1 = DiscountInfo.builder() + .title("할인정보1") + .startDate(LocalDate.of(2024, 7, 1)) + .endDate(LocalDate.of(2024, 7, 21)) + .discountRate(40) + .siteUrl("http://example1.com") + .build(); + + DiscountInfo discount2 = DiscountInfo.builder() + .title("할인정보2") + .startDate(LocalDate.of(2024, 7, 1)) + .endDate(LocalDate.of(2024, 7, 21)) + .discountRate(40) + .siteUrl("http://example1.com") + .build(); + + discountInfoRepository.save(discount1); + discountInfoRepository.save(discount2); + + // when + discountInfoRepository.deleteById(1L); + + // then + assertThat(discountInfoRepository.findAll()).hasSize(1); + assertThat(discountInfoRepository.findAll().get(0).getTitle()).isEqualTo("할인정보2"); + } + @Test @DisplayName("특정 년월에 속하는 할인 정보 데이터 조회 성공") void findByDateRangeSuccess() { diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/supportinfo/repository/SupportInfoRepositoryTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/supportinfo/repository/SupportInfoRepositoryTest.java index 8032ac12..9bb09ce0 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/supportinfo/repository/SupportInfoRepositoryTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/supportinfo/repository/SupportInfoRepositoryTest.java @@ -25,7 +25,29 @@ class SupportInfoRepositoryTest { @Autowired - SupportInfoRepository supportInfoRepository; + private SupportInfoRepository supportInfoRepository; + + @Test + @DisplayName("@SoftDelete 테스트") + void deletedTest() { + // given + SupportInfo discount2 = SupportInfo.builder() + .title("지원정보2") + .startDate(LocalDate.of(2024, 7, 1)) + .endDate(LocalDate.of(2024, 7, 21)) + .siteUrl("http://example1.com") + .build(); + + supportInfoRepository.save(discount1); + supportInfoRepository.save(discount2); + + // when + supportInfoRepository.deleteById(1L); + + // then + assertThat(supportInfoRepository.findAll()).hasSize(1); + assertThat(supportInfoRepository.findAll().get(0).getTitle()).isEqualTo("지원정보2"); + } @Test @DisplayName("특정 년월에 속하는 지원 정보 데이터 조회 성공") @@ -37,6 +59,7 @@ void findByDateRangeSuccess() { .endDate(LocalDate.of(2024, 7, 21)) .siteUrl("http://example1.com") .build(); + supportInfoRepository.save(discount1); // when