Skip to content

Commit

Permalink
Merge pull request #249 from linkarchive/fix/user
Browse files Browse the repository at this point in the history
유저 관련 연관 테이블의 영속성 전이를 수정해줬어요
  • Loading branch information
taegyun1995 authored Aug 18, 2023
2 parents 6548aad + 04961d5 commit 1000b80
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ public LoginResponse login(String code, String referer, String userAgent) {
KakaoProfile kakaoProfile = jwtUtil.getUserInfo(oauthToken.getAccess_token());

User findUser = userRepository.findBySocialId(kakaoProfile.getId())
.orElseGet(() -> {
ProfileImage profileImage = ProfileImage.create(DEFAULT_IMAGE);
Pin pin = Pin.create();
User user = User.create(KAKAO, kakaoProfile, profileImage, pin);
userRepository.save(user);
.orElseGet(
() -> {
ProfileImage profileImage = ProfileImage.create(DEFAULT_IMAGE);
Pin pin = Pin.create();
User user = User.create(KAKAO, kakaoProfile, profileImage, pin);
userRepository.save(user);

return user;
});
return user;
}
);

findUser.updateAuthProvider(KAKAO);
String newAccessToken = jwtUtil.createAccessToken(findUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ public LocalLoginService(JwtUtil jwtUtil, UserRepository userRepository, Refresh

public LoginResponse login(String userAgent) {
User findUser = userRepository.findBySocialId(SOCIAL_LOGIN)
.orElseGet(() -> {
ProfileImage profileImage = ProfileImage.create(DEFAULT_IMAGE);
Pin pin = Pin.create();
User user = User.localCreate(profileImage, pin);
userRepository.save(user);

return user;
});
.orElseGet(
() -> {
ProfileImage profileImage = ProfileImage.create(DEFAULT_IMAGE);
Pin pin = Pin.create();
User user = User.localCreate(profileImage, pin);
userRepository.save(user);

return user;
}
);

String newAccessToken = jwtUtil.createAccessToken(findUser);
String newRefreshToken = jwtUtil.createRefreshToken(findUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Pin extends TimeEntity {
private int linkCount;
private int bookmarkCount;

@OneToOne(mappedBy = "profileImage", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OneToOne(mappedBy = "pin", fetch = FetchType.LAZY)
private User user;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ProfileImage extends TimeEntity {

private String fileName;

@OneToOne(mappedBy = "profileImage", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OneToOne(mappedBy = "profileImage", fetch = FetchType.LAZY)
private User user;

@Builder
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/project/linkarchive/backend/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import lombok.NoArgsConstructor;
import project.linkarchive.backend.advice.entityBase.TimeEntity;
import project.linkarchive.backend.auth.AuthProvider;
import project.linkarchive.backend.auth.domain.RefreshToken;
import project.linkarchive.backend.auth.response.KakaoProfile;
import project.linkarchive.backend.bookmark.domain.Bookmark;
import project.linkarchive.backend.hashtag.domain.UserHashtag;
import project.linkarchive.backend.isLinkRead.domain.IsLinkRead;
import project.linkarchive.backend.link.domain.Link;
import project.linkarchive.backend.notification.domain.Notification;
import project.linkarchive.backend.notification.enums.NotificationPreference;
import project.linkarchive.backend.pin.domain.Pin;
Expand Down Expand Up @@ -50,24 +52,30 @@ public class User extends TimeEntity {
@Enumerated(EnumType.STRING)
private NotificationPreference notificationPreference;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name = "profile_image_id")
private ProfileImage profileImage;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name = "pin_id")
private Pin pin;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
private RefreshToken refreshToken;

@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<Link> linkList = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<UserHashtag> userHashtagList = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<Bookmark> bookmarkList = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<IsLinkRead> isLinkReadList = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<Notification> notificationList = new ArrayList<>();

@Builder
Expand Down

0 comments on commit 1000b80

Please sign in to comment.