1

I'm stuck at deal with this problem. I have 'Review Entity', and 'Heart Entitiy'. And I tried to show them homepage and detailpage separately!

Long countHeartByBookReviewId(Long bookReview_id);

i used jpa query method for showing how many heart it gets in details page..

and now i want to show review descending related to heart count in main page!

how can i make the code..?

@Entity
public class BookReview extends Timestamped {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;

...

@Column
private String review;

@JoinColumn(name = "member_id", nullable = false)
@ManyToOne(fetch = FetchType.EAGER)
private Member member;

@OneToMany(mappedBy = "bookReview" , cascade = CascadeType.REMOVE)
private List<Comment> comment;

@JsonIgnore
@OneToMany(mappedBy = "bookReview", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Heart> heart;

and the other entitiy is here.

public class Heart {

@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "bookReview_id")
private BookReview bookReview;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

and this is function for get menthod...

    public ResponseDto<?> getHome() {
    List<BookReview> book_review = book_reviewRepository.findAllByOrderByHeartDesc();
    List<HomeResponseDto> book_reviewResponseDtoList = new ArrayList<>();

    for (BookReview home : book_review) {
        book_reviewResponseDtoList.add(HomeResponseDto.builder()
                        .id(home.getId())
                        .username(home.getMember().getUsername())
                        .thumbnail(home.getThumbnail())
                        .title(home.getTitle())
                        .author(home.getAuthor())
                        .publisher(home.getPublisher())
                        .review(home.getReview())
                        .heart(heartRepository.countHeartByBookReviewId(home.getId()))
                        .createdAt(home.getCreatedAt())
                        .modifiedAt(home.getModifiedAt())
                        .build()
                );

    }
    return ResponseDto.success(book_reviewResponseDtoList);
}

please help me ......

Vy Do
  • 46,709
  • 59
  • 215
  • 313
sejin park
  • 11
  • 2
  • Welcome to SO. You might want to do the following first: 1) read [ask], 2) add some description of what you're referring to by "how can i make the code" - are you asking how to write the query or how to name the Spring repo method so Spring data knows what you want? 3) Did you read the Spring data documentation already, especially the section about query inference from method names (should be section 4.4)? – Thomas Sep 07 '22 at 08:04
  • One more question: since a review can have multiple "hearts" how would you order them? Do you want to order by the number of hearts? This might help you: https://stackoverflow.com/questions/23136469/order-by-count-using-spring-data-jparepository – Thomas Sep 07 '22 at 08:18

0 Answers0