Skip to content

Commit 87b6e2c

Browse files
committed
fix: 연관관계 수정
- 양방향 매핑으로 일단 Cascade 작동하게 해놓음 - 자식 Entity에서 Cascade 제거
1 parent 00b4aea commit 87b6e2c

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

src/main/java/org/dfbf/soundlink/domain/blocklist/entity/Blocklist.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class Blocklist {
2020
@GeneratedValue(strategy = GenerationType.IDENTITY)
2121
private Long blocklistId;
2222

23-
@ManyToOne (cascade = CascadeType.REMOVE)
23+
@ManyToOne
2424
@JoinColumn(name = "user_id")
2525
private User user;
2626

27-
@ManyToOne (cascade = CascadeType.REMOVE)
27+
@ManyToOne
2828
@JoinColumn(name = "blocked_user_id")
2929
private User blockedUser;
3030

src/main/java/org/dfbf/soundlink/domain/emotionRecord/entity/EmotionRecord.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class EmotionRecord {
1818
@GeneratedValue(strategy = GenerationType.IDENTITY)
1919
private Long recordId;
2020

21-
@ManyToOne(cascade = CascadeType.REMOVE)
21+
@ManyToOne
2222
@JoinColumn(name = "user_id")
2323
private User user;
2424

@@ -44,10 +44,3 @@ public EmotionRecord(User user, Emotions emotion, String comment) {
4444
this.comment = comment;
4545
}
4646
}
47-
48-
/**
49-
* PERSIST - 부모와 자식엔티티를 한 번에 영속화
50-
* REMOVE - 함께 저장했던 부모와 자식의 엔티티를 모두 제거할 경우 (고아로 만듭니다)
51-
* ALL - CascadeType.PERSIST 와 CascadeType.REMOVE 동시 적용
52-
* orphanRemoval=true -> 해당 고아 객체를 자동으로 삭제해 주는 옵션
53-
*/

src/main/java/org/dfbf/soundlink/domain/user/controller/UserController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ public ResponseResult checkNickName(@RequestParam String nickName){
3434

3535
@GetMapping
3636
@Operation(summary = "유저 조회", description = "유저 조회 API")
37-
public ResponseResult getUser(/*@AuthenticationPrincipal id: Int*/) { return userService.getUser(1L); }
37+
public ResponseResult getUser(/*@AuthenticationPrincipal id: Long*/) { return userService.getUser(1L); }
3838

3939
@PutMapping
4040
@Operation(summary = "유저 수정", description = "유저 수정 API")
41-
public ResponseResult updateUser(/*@AuthenticationPrincipal id: Int, */@RequestBody UserUpdateDto userUpdateDto) {
41+
public ResponseResult updateUser(/*@AuthenticationPrincipal id: Long, */@RequestBody UserUpdateDto userUpdateDto) {
4242
return userService.updateUser(1L, userUpdateDto);
4343
}
4444

4545
@DeleteMapping
4646
@Operation(summary = "유저 삭제", description = "회원 탈퇴하는 API (탈퇴시 프로필 정보도 삭제됩니다.)")
47-
public ResponseResult deleteUser(/*@AuthenticationPrincipal id: Int*/) { return userService.deleteUser(1L); }
47+
public ResponseResult deleteUser(/*@AuthenticationPrincipal id: Long*/) { return userService.deleteUser(1L); }
4848

4949
@GetMapping("/mypage")
5050
@Operation(summary = "마이 페이지", description = "마이 페이지 조회 API")
51-
public ResponseResult getMyPage(/*@AuthenticationPrincipal id: Int*/) { return userService.getMyPage(1L); }
51+
public ResponseResult getMyPage(/*@AuthenticationPrincipal id: Long*/) { return userService.getMyPage(1L); }
5252
}

src/main/java/org/dfbf/soundlink/domain/user/dto/response/EmotionRecordDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public record EmotionRecordDto(Long spotifyId, String title, String artist, String album, Emotions emotion, String comment ,String createdAt) {
99
// 커스텀 생성자 -> createdAt을 timestamp에서 String으로
10-
public EmotionRecordDto(Long spotifyId, String title, String artist, String album, Emotions emotion, String comment ,Timestamp createdAt) {
10+
public EmotionRecordDto(Long spotifyId, String title, String artist, String album, Emotions emotion, String comment, Timestamp createdAt) {
1111
this(spotifyId, title, artist, album, emotion, comment, formatTimestamp(createdAt));
1212
}
1313

src/main/java/org/dfbf/soundlink/domain/user/entity/ProfileMusic.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.persistence.Id;
55
import lombok.Builder;
66
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
78
import org.dfbf.soundlink.domain.emotionRecord.entity.SpotifyMusic;
89
import org.hibernate.annotations.CreationTimestamp;
910
import org.hibernate.annotations.UpdateTimestamp;
@@ -12,6 +13,7 @@
1213

1314
@Entity
1415
@Getter
16+
@NoArgsConstructor
1517
public class ProfileMusic {
1618
@Id
1719
@GeneratedValue(strategy = GenerationType.IDENTITY)

src/main/java/org/dfbf/soundlink/domain/user/entity/User.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import jakarta.persistence.*;
44
import lombok.*;
5+
import org.dfbf.soundlink.domain.blocklist.entity.Blocklist;
6+
import org.dfbf.soundlink.domain.emotionRecord.entity.EmotionRecord;
57
import org.dfbf.soundlink.domain.user.dto.request.UserUpdateDto;
68
import org.dfbf.soundlink.global.comm.enums.SocialType;
79
import org.hibernate.annotations.CreationTimestamp;
810
import org.hibernate.annotations.UpdateTimestamp;
911
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1012

1113
import java.sql.Timestamp;
14+
import java.util.List;
1215

1316
@Entity
1417
@Getter
@@ -31,6 +34,15 @@ public class User {
3134
private String password;
3235
private String email;
3336

37+
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
38+
private List<Blocklist> blocklist;
39+
40+
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
41+
private List<EmotionRecord> emotionRecord;
42+
43+
@OneToOne(mappedBy = "user", cascade = CascadeType.REMOVE)
44+
private ProfileMusic profileMusic;
45+
3446
@CreationTimestamp
3547
private Timestamp createdAt;
3648
@UpdateTimestamp
@@ -53,3 +65,20 @@ public void update(UserUpdateDto userUpdateDto, BCryptPasswordEncoder passwordEn
5365
this.email = userUpdateDto.email();
5466
}
5567
}
68+
69+
/**
70+
* PERSIST - 부모 엔티티가 영속화(저장)될 때, 자식 엔티티도 자동으로 저장
71+
* MERGE - 부모 엔티티가 병합될 때, 자식 엔티티도 자동으로 병합
72+
* REMOVE - 부모 엔티티가 삭제될 때, 자식 엔티티도 자동으로 삭제
73+
* REFRESH - 부모 엔티티가 새로고침(refresh)될 때, 자식 엔티티도 자동으로 새로고침
74+
* DETACH - 부모 엔티티가 분리(detach)될 때, 자식 엔티티도 자동으로 분리
75+
* ALL - 모든 작업을 자식 엔티티에 전파
76+
*/
77+
78+
/**
79+
* CascadeType.REMOVE -> 부모 엔티티가 삭제될 때 자식 엔티티는 삭제
80+
* orphanRemoval = true -> 자식 엔티티가 부모와의 관계에서 제거될 때 삭제
81+
* 가끔 사용하지 않는 데이터가 DB에 남아있는 경우가 있는데, 이를 방지하기 위해 사용
82+
* 혹은 User쪽에 있는 리스트에서 제거할 경우, 양방향 매핑이기 떄문에 자식이 고아가 댐 -> 이를 위헤서 orphanRemoval를 사용
83+
* 근데 우린 사용 안하니까 사실 빼도 무방할 듯....
84+
*/

0 commit comments

Comments
 (0)