[Spring JPA #11] 쿼리 실습
- 📚 Spring/Spring JPA
- 2020. 7. 19. 10:28
package org.kyhslam.jpa.post;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Comment {
@Id @GeneratedValue
private Long id;
private String comment;
@ManyToOne
private Post post;
private Integer likeCount;
public Integer getLikeCount() {
return likeCount;
}
public void setLikeCount(Integer likeCount) {
this.likeCount = likeCount;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
}
package org.kyhslam.jpa.post;
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Post {
@Id @GeneratedValue
private Long id;
private String title;
@OneToMany(mappedBy = "post")
private Set<Comment> comments = new HashSet<>();
public Set<Comment> getComments() {
return comments;
}
public void setComments(Set<Comment> comments) {
this.comments = comments;
}
@Lob
private String content;
@Temporal(TemporalType.TIMESTAMP)
private Date created;
}
package org.kyhslam.jpa.post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CommentRepository extends JpaRepository<Comment, Long> {
Page<Comment> findByCommentContainsIgnoreCase(String keyword, Pageable pageable);
}
테스트
package org.kyhslam.jpa.post;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@DataJpaTest
public class CommentRepositoryTest {
@Autowired
CommentRepository commentRepository;
@Test
public void crud(){
this.createComment(100, "spring data jpa");
this.createComment(55, "HIBERNATE spring");
PageRequest pageRequest = PageRequest.of(0,10, Sort.by(Sort.Direction.DESC, "LikeCount"));
Page<Comment> comments = commentRepository.findByCommentContainsIgnoreCase("Spring", pageRequest);
//Assertions.assertThat(comments.getNumberOfElements()).isEqualTo(2);
//Assertions.assertThat(comments).first().hasFieldOrPropertyWithValue("likeCount", 55);
System.out.println("Page SIZE : " + comments.getSize());
System.out.println("Total PAGES : " + comments.getTotalPages());
System.out.println("Total COUNT : " + comments.getTotalElements());
System.out.println("NEXT : " + comments.nextPageable());
List<Comment> list = comments.getContent();
list.forEach(comment -> System.out.println(comment.getId() + " - " + comment.getComment()));
}
private void createComment(int likeCount, String comment){
Comment newComment = new Comment();
newComment.setLikeCount(likeCount);
newComment.setComment(comment);
commentRepository.save(newComment);
}
}
select
comment0_.id as id1_0_,
comment0_.comment as comment2_0_,
comment0_.like_count as like_cou3_0_,
comment0_.post_id as post_id4_0_
from
comment comment0_
where
upper(comment0_.comment) like upper(?)
order by
comment0_.like_count desc limit ?
Page SIZE : 10
Total PAGES : 1
Total COUNT : 2
NEXT : INSTANCE
1 - spring data jpa
'📚 Spring > Spring JPA' 카테고리의 다른 글
@JsonIgnore (0) | 2020.09.04 |
---|---|
[Spring JPA #12] 커스텀 리포지토리 (0) | 2020.07.22 |
[Spring JPA #7] Query (0) | 2020.07.15 |
[Spring JPA #6] Fetch (0) | 2020.07.15 |
[Spring JPA #5] 엔티티 상태와 Cascade (0) | 2020.07.14 |