Skip to content

Commit 8769179

Browse files
authored
Merge branch 'dev' into fix/287
2 parents b23ba21 + 3bff555 commit 8769179

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

src/main/java/com/back/domain/file/entity/AttachmentMapping.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
@Getter
1010
@NoArgsConstructor
1111
public class AttachmentMapping extends BaseEntity {
12-
@ManyToOne(fetch = FetchType.LAZY)
13-
@JoinColumn(name = "attachment_id")
12+
@OneToOne(fetch = FetchType.LAZY, mappedBy = "attachmentMapping", cascade = CascadeType.ALL, orphanRemoval = true)
1413
private FileAttachment fileAttachment;
1514

1615
@Enumerated(EnumType.STRING)

src/main/java/com/back/domain/file/entity/FileAttachment.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public class FileAttachment extends BaseEntity {
2929
@JoinColumn(name = "uploaded_by")
3030
private User user;
3131

32-
@OneToMany(mappedBy = "fileAttachment", cascade = CascadeType.ALL, orphanRemoval = true)
33-
private List<AttachmentMapping> attachmentMappings = new ArrayList<>();
32+
@OneToOne(fetch = FetchType.LAZY)
33+
@JoinColumn(name = "attachmentMapping_id")
34+
private AttachmentMapping attachmentMapping;
3435

3536
public FileAttachment(
3637
String storedName,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.back.domain.file.repository;
2+
3+
import com.back.domain.file.entity.AttachmentMapping;
4+
import com.back.domain.file.entity.EntityType;
5+
import com.back.domain.file.entity.FileAttachment;
6+
import jakarta.persistence.EntityManager;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.mock.web.MockMultipartFile;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@SpringBootTest
17+
@Transactional
18+
class AttachmentMappingRepositoryTest {
19+
@Autowired
20+
private FileAttachmentRepository fileAttachmentRepository;
21+
@Autowired
22+
private AttachmentMappingRepository attachmentMappingRepository;
23+
@Autowired
24+
private EntityManager em;
25+
26+
@Test
27+
void AttachmentMapping_삭제시_FileAttachment_자동삭제_확인() throws Exception {
28+
// given
29+
FileAttachment fileAttachment = new FileAttachment(
30+
"test",
31+
new MockMultipartFile("test", "test", "image", "test".getBytes()),
32+
null,
33+
"test.URL"
34+
);
35+
fileAttachmentRepository.save(fileAttachment);
36+
System.out.println("현재 저장된 파일 개수 : " + fileAttachmentRepository.findAll().size());
37+
38+
AttachmentMapping attachmentMapping = new AttachmentMapping(
39+
fileAttachment,
40+
EntityType.POST,
41+
1L
42+
);
43+
attachmentMappingRepository.save(attachmentMapping);
44+
45+
// when
46+
attachmentMappingRepository.deleteAllByEntityTypeAndEntityId(EntityType.POST, 1L);
47+
48+
em.flush(); // 즉시 DB에 변경사항 반영
49+
em.clear(); // 영속성 컨텍스트 초기화
50+
51+
// then
52+
assertThat(fileAttachmentRepository.findAll().size()).isEqualTo(0);
53+
}
54+
}

0 commit comments

Comments
 (0)