API 정보는 이전 정리한 페이지 참조
1. 회원 가입
로직
@Transactional // exception나면 rollback 위한 어노테이션
public User join(String userName, String password){
// 회원가입하려하는 userName으로 회원가입된 user가 있는지
userEntityRepository.findByUserName(userName).ifPresent(it ->{
throw new SnsApplicationException(ErrorCode.DUPLICATED_USER_NAME, String.format("%s is duplicated", userName));
});
// 회원가입 진행 = user를 등록
UserEntity userEntity = userEntityRepository.save(UserEntity.of(userName, encoder.encode(password)));
return User.fromEntity(userEntity);
}
수행되는 하이버네이트 쿼리
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
insert
into
`
user` (
deleted_at, password, registered_at, role, updated_at, user_name
)
values
(?, ?, ?, ?, ?, ?)
2. 로그인
redis 캐싱되어 있어서 쿼리 못 봄.
3. 포스트 등록
@Transactional
public void create(String title, String body, String userName){
// user find
UserEntity userEntity = getUserEntityOrException(userName);
// post save
postEntityRepository.save(PostEntity.of(title, body, userEntity));
}
수행되는 하이버네이트 쿼리
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
insert
into
post
(body, deleted_at, registered_at, title, updated_at, user_id)
values
(?, ?, ?, ?, ?, ?)
4. 포스트 수정
@Transactional
public Post modify(String title, String body, String userName, Integer postId){
UserEntity userEntity = getUserEntityOrException(userName);
// post exist
PostEntity postEntity = getPostEntityOrException(postId);
// post permission
if(postEntity.getUser() != userEntity){
throw new SnsApplicationException(ErrorCode.INVALID_PERMISSION, String.format("%s has no permission with %s", userName, postId));
}
postEntity.setTitle(title);
postEntity.setBody(body);
return Post.fromEntity(postEntityRepository.saveAndFlush(postEntity));
}
수행되는 하이버네이트 쿼리
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
select
postentity0_.id as id1_3_0_,
postentity0_.body as body2_3_0_,
postentity0_.deleted_at as deleted_3_3_0_,
postentity0_.registered_at as register4_3_0_,
postentity0_.title as title5_3_0_,
postentity0_.updated_at as updated_6_3_0_,
postentity0_.user_id as user_id7_3_0_,
userentity1_.id as id1_4_1_,
userentity1_.deleted_at as deleted_2_4_1_,
userentity1_.password as password3_4_1_,
userentity1_.registered_at as register4_4_1_,
userentity1_.role as role5_4_1_,
userentity1_.updated_at as updated_6_4_1_,
userentity1_.user_name as user_nam7_4_1_
from
post postentity0_
left outer join
`user` userentity1_
on postentity0_.user_id=userentity1_.id
where
postentity0_.id=?
and (
postentity0_.deleted_at is NULL
)
Hibernate:
update
post
set
body=?,
deleted_at=?,
registered_at=?,
title=?,
updated_at=?,
user_id=?
where
id=?
5. 포스트 삭제
@Transactional
public void delete(String userName, Integer postId){
UserEntity userEntity = getUserEntityOrException(userName);
// post exist
PostEntity postEntity = getPostEntityOrException(postId);
// post permission
if(postEntity.getUser() != userEntity){
throw new SnsApplicationException(ErrorCode.INVALID_PERMISSION, String.format("%s has no permission with %s", userName, postId));
}
likeEntityRepository.deleteAllByPost(postEntity);
commentEntityRepository.deleteAllByPost(postEntity);
postEntityRepository.delete(postEntity);
}
수행되는 하이버네이트 쿼리
PostEntity 가져올 땐 @ManyToOne 되어 있어서 SELECT 문에 USER Entity 값도 포함되어 있는 듯 하다.
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
select
postentity0_.id as id1_3_0_,
postentity0_.body as body2_3_0_,
postentity0_.deleted_at as deleted_3_3_0_,
postentity0_.registered_at as register4_3_0_,
postentity0_.title as title5_3_0_,
postentity0_.updated_at as updated_6_3_0_,
postentity0_.user_id as user_id7_3_0_,
userentity1_.id as id1_4_1_,
userentity1_.deleted_at as deleted_2_4_1_,
userentity1_.password as password3_4_1_,
userentity1_.registered_at as register4_4_1_,
userentity1_.role as role5_4_1_,
userentity1_.updated_at as updated_6_4_1_,
userentity1_.user_name as user_nam7_4_1_
from
post postentity0_
left outer join
`user` userentity1_
on postentity0_.user_id=userentity1_.id
where
postentity0_.id=?
and (
postentity0_.deleted_at is NULL
)
Hibernate:
update
liked
set
deleted_at=now(6)
where
post_id=?
Hibernate:
update
comment
set
deleted_at=now(6)
where
post_id=?
Hibernate:
UPDATE
post
SET
deleted_at = NOW()
where
id=?
6. 피드 조회
public Page<Post> my(String userName, Pageable pageable){
UserEntity userEntity = getUserEntityOrException(userName);
return postEntityRepository.findAllByUser(userEntity, pageable).map(Post::fromEntity);
}
수행되는 하이버네이트 쿼리
페이징이 있어서 limit 쿼리가 추가된 것 확인 가능
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
select
postentity0_.id as id1_3_,
postentity0_.body as body2_3_,
postentity0_.deleted_at as deleted_3_3_,
postentity0_.registered_at as register4_3_,
postentity0_.title as title5_3_,
postentity0_.updated_at as updated_6_3_,
postentity0_.user_id as user_id7_3_
from
post postentity0_
where
(
postentity0_.deleted_at is NULL
)
and postentity0_.user_id=? limit ?
7. 좋아요 기능
@Transactional
public void like(Integer postId, String userName){
UserEntity userEntity = getUserEntityOrException(userName);
PostEntity postEntity = getPostEntityOrException(postId);
// check liked -> throw
// likeEntityRepository
likeEntityRepository.findByUserAndPost(userEntity, postEntity).ifPresent(it->{
throw new SnsApplicationException(ErrorCode.ALREADY_LIKED, String.format("userName %s already like post %d", userName, postId));
});
// like save
likeEntityRepository.save(LikeEntity.of(userEntity, postEntity));
AlarmEntity alarmEntity = alarmEntityRepository.save(AlarmEntity.of(postEntity.getUser(), AlarmType.NEW_LIKE_ON_POST, new AlarmArgs(userEntity.getId(), postEntity.getId())));
alarmService.send(alarmEntity.getId(), postEntity.getUser().getId());
}
수행되는 하이버네이트 쿼리
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
select
postentity0_.id as id1_3_0_,
postentity0_.body as body2_3_0_,
postentity0_.deleted_at as deleted_3_3_0_,
postentity0_.registered_at as register4_3_0_,
postentity0_.title as title5_3_0_,
postentity0_.updated_at as updated_6_3_0_,
postentity0_.user_id as user_id7_3_0_,
userentity1_.id as id1_4_1_,
userentity1_.deleted_at as deleted_2_4_1_,
userentity1_.password as password3_4_1_,
userentity1_.registered_at as register4_4_1_,
userentity1_.role as role5_4_1_,
userentity1_.updated_at as updated_6_4_1_,
userentity1_.user_name as user_nam7_4_1_
from
post postentity0_
left outer join
`user` userentity1_
on postentity0_.user_id=userentity1_.id
where
postentity0_.id=?
and (
postentity0_.deleted_at is NULL
)
Hibernate:
select
likeentity0_.id as id1_2_,
likeentity0_.deleted_at as deleted_2_2_,
likeentity0_.post_id as post_id5_2_,
likeentity0_.registered_at as register3_2_,
likeentity0_.updated_at as updated_4_2_,
likeentity0_.user_id as user_id6_2_
from
liked likeentity0_
where
(
likeentity0_.deleted_at is NULL
)
and likeentity0_.user_id=?
and likeentity0_.post_id=?
Hibernate:
insert
into
liked
(deleted_at, post_id, registered_at, updated_at, user_id)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
alarm
(alarm_type, args, deleted_at, post_id, registered_at, updated_at, user_id)
values
(?, ?, ?, ?, ?, ?, ?)
2024-01-04 04:45:41.989 INFO 28156 --- [nio-9090-exec-1] c.e.sns.repository.EmitterRepository : Get sseEmitter 1
2024-01-04 04:45:41.989 INFO 28156 --- [nio-9090-exec-1] com.example.sns.service.AlarmService : No emitter founded
8. 좋아요 개수 조회
@Transactional
public long likeCount(Integer postId){
PostEntity postEntity = getPostEntityOrException(postId);
// like save
return likeEntityRepository.countByPost(postEntity);
}
수행되는 하이버네이트 쿼리
Hibernate:
select
postentity0_.id as id1_3_0_,
postentity0_.body as body2_3_0_,
postentity0_.deleted_at as deleted_3_3_0_,
postentity0_.registered_at as register4_3_0_,
postentity0_.title as title5_3_0_,
postentity0_.updated_at as updated_6_3_0_,
postentity0_.user_id as user_id7_3_0_,
userentity1_.id as id1_4_1_,
userentity1_.deleted_at as deleted_2_4_1_,
userentity1_.password as password3_4_1_,
userentity1_.registered_at as register4_4_1_,
userentity1_.role as role5_4_1_,
userentity1_.updated_at as updated_6_4_1_,
userentity1_.user_name as user_nam7_4_1_
from
post postentity0_
left outer join
`user` userentity1_
on postentity0_.user_id=userentity1_.id
where
postentity0_.id=?
and (
postentity0_.deleted_at is NULL
)
Hibernate:
select
count(*) as col_0_0_
from
liked likeentity0_
where
(
likeentity0_.deleted_at is NULL
)
and likeentity0_.post_id=?
9. 코멘트 생성
@Transactional
public void comment(Integer postId, String userName, String comment){
UserEntity userEntity = getUserEntityOrException(userName);
PostEntity postEntity = getPostEntityOrException(postId);
commentEntityRepository.save(CommentEntity.of(userEntity, postEntity, comment));
AlarmEntity alarmEntity = alarmEntityRepository.save(AlarmEntity.of(postEntity.getUser(), AlarmType.NEW_COMMENT_ON_POST, new AlarmArgs(userEntity.getId(), postEntity.getId())));
alarmService.send(alarmEntity.getId(), postEntity.getUser().getId());
}
수행되는 하이버네이트 쿼리
Hibernate:
select
userentity0_.id as id1_4_,
userentity0_.deleted_at as deleted_2_4_,
userentity0_.password as password3_4_,
userentity0_.registered_at as register4_4_,
userentity0_.role as role5_4_,
userentity0_.updated_at as updated_6_4_,
userentity0_.user_name as user_nam7_4_
from
`user` userentity0_
where
(
userentity0_.deleted_at is NULL
)
and userentity0_.user_name=?
Hibernate:
select
postentity0_.id as id1_3_0_,
postentity0_.body as body2_3_0_,
postentity0_.deleted_at as deleted_3_3_0_,
postentity0_.registered_at as register4_3_0_,
postentity0_.title as title5_3_0_,
postentity0_.updated_at as updated_6_3_0_,
postentity0_.user_id as user_id7_3_0_,
userentity1_.id as id1_4_1_,
userentity1_.deleted_at as deleted_2_4_1_,
userentity1_.password as password3_4_1_,
userentity1_.registered_at as register4_4_1_,
userentity1_.role as role5_4_1_,
userentity1_.updated_at as updated_6_4_1_,
userentity1_.user_name as user_nam7_4_1_
from
post postentity0_
left outer join
`user` userentity1_
on postentity0_.user_id=userentity1_.id
where
postentity0_.id=?
and (
postentity0_.deleted_at is NULL
)
Hibernate:
insert
into
comment
(comment, deleted_at, post_id, registered_at, updated_at, user_id)
values
(?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
alarm
(alarm_type, args, deleted_at, post_id, registered_at, updated_at, user_id)
values
(?, ?, ?, ?, ?, ?, ?)
10. 코멘트 조회
public Page<Comment> getComments(Integer postId, Pageable pageable){
PostEntity postEntity = getPostEntityOrException(postId);
return commentEntityRepository.findByPostId(pageable, postEntity).map(Comment::fromEntity);
}
수행되는 하이버네이트 쿼리
Hibernate:
select
postentity0_.id as id1_3_0_,
postentity0_.body as body2_3_0_,
postentity0_.deleted_at as deleted_3_3_0_,
postentity0_.registered_at as register4_3_0_,
postentity0_.title as title5_3_0_,
postentity0_.updated_at as updated_6_3_0_,
postentity0_.user_id as user_id7_3_0_,
userentity1_.id as id1_4_1_,
userentity1_.deleted_at as deleted_2_4_1_,
userentity1_.password as password3_4_1_,
userentity1_.registered_at as register4_4_1_,
userentity1_.role as role5_4_1_,
userentity1_.updated_at as updated_6_4_1_,
userentity1_.user_name as user_nam7_4_1_
from
post postentity0_
left outer join
`user` userentity1_
on postentity0_.user_id=userentity1_.id
where
postentity0_.id=?
and (
postentity0_.deleted_at is NULL
)
Hibernate:
select
commentent0_.id as id1_1_,
commentent0_.comment as comment2_1_,
commentent0_.deleted_at as deleted_3_1_,
commentent0_.post_id as post_id6_1_,
commentent0_.registered_at as register4_1_,
commentent0_.updated_at as updated_5_1_,
commentent0_.user_id as user_id7_1_
from
comment commentent0_
where
(
commentent0_.deleted_at is NULL
)
and commentent0_.post_id=? limit ?
'sns' 카테고리의 다른 글
서버 부팅 시 Table 자동 생성되는 Hibernate 쿼리 기록 (0) | 2024.01.04 |
---|---|
프로젝트 api 정리 (0) | 2023.12.30 |
댓글