Skip to content

Commit

Permalink
Merge pull request #52 from BudgetBuddiesTeam/refactor/#17
Browse files Browse the repository at this point in the history
[refactor] 즉시 로딩을 지연 로딩으로 변경
  • Loading branch information
SoulTree-Lovers authored Jul 28, 2024
2 parents f84a4e8 + 1343316 commit 92d0374
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +20,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@SuperBuilder
@SoftDelete // boolean 타입의 deleted 필드가 추가
@SoftDelete(columnName = "deleted") // boolean 타입의 deleted 필드가 추가
@Getter
public abstract class BaseEntity {

Expand All @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Where;

import java.time.LocalDateTime;

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("특정 년월에 속하는 지원 정보 데이터 조회 성공")
Expand All @@ -37,6 +59,7 @@ void findByDateRangeSuccess() {
.endDate(LocalDate.of(2024, 7, 21))
.siteUrl("http://example1.com")
.build();

supportInfoRepository.save(discount1);

// when
Expand Down

0 comments on commit 92d0374

Please sign in to comment.