I want to find the list of Child using parent’s id using spring data jpa.
I know there are two ways to retrieve data from spring data jpa, which is
using
- derived queryies
- query methods like findBy….()
or using a native query @Query(“[]”).
These are the two entities I need. Post
, and Reply
Entitiy.
Post
is ManyToOne to Reply
, and Reply
is OneToMany To Post
.
I also have a Member entity in oneToManyRelation from Post Entity which is not used in this case though.
Post
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@ToString(exclude = {
"member"}
) public class Post extends BaseEntity {
@Id
@GeneratedValue
private Long postId;
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") private Member member;
@Column(nullable = false, columnDefinition = "TINYTEXT") private String title;
@Column(nullable = false, columnDefinition = "TEXT") private String content;
@Column(nullable = false) private Long views = 0L;
@OneToMany(mappedBy = "post") private List<Reply> replies;
}
Reply
@Entity
@NoArgsConstructor @AllArgsConstructor @Builder
@Getter
@Setter
@ToString(exclude = {"member"})
public class Reply extends BaseEntity {
@Id @GeneratedValue
private Long replyId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
@Column(length = 500)
private String replyText;
}
I want to query like code below work. However, this native query shows error.
@Query("SELECT p FROM Reply r JOIN Post p WHERE r.post.postId= :postid")
List<Reply> findRepliesByPostId(@Param("postId") Long postId, Sort sort);
Error
Could not create query for public abstract java.util.List com.cms.mini_board.repository.ReplyRepository.findRepliesByPostId(java.lang.Long,org.springframework.data.domain.Sort); Reason: Using named parameters for method public abstract java.util.List com.cms.mini_board.repository.ReplyRepository.findRepliesByPostId(java.lang.Long,org.springframework.data.domain.Sort) but parameter 'Optional[postId]' not found in annotated query 'SELECT p FROM Reply r JOIN Post p WHERE r.post.postId= :postid'
I have succeeded in returning what I wanted List<Reply>
by operating in Post Entity
in postRepository.
However, I want the same result operating in Reply Repository because I think it suits the meaning of “searching from Reply Table where postId = postId”. Which is the case when using RDBMS. Below is the code I succeeded using Post
Entity.
@Override
public List<ReplyDTO> getReplyList(Long postId) {
Post postResult = postRepository.findById(postId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NO_CONTENT, "No such element exception"));
List<ReplyDTO> replyDTOS = postResult.getReplies().stream().map(reply ->
replyEntityToDTO(reply)
).collect(Collectors.toList());
return replyDTOS;
}
So the final question is,
- Can I do this in query methods / derived query methods?
- Or Is there any method to do this?
- I can’t find why my code below doesn’t work. can someone tell the solution?
@Query("SELECT p FROM Reply r JOIN Post p WHERE r.post.postId= :postid")
List<Reply> findRepliesByPostId(@Param("postId") Long postId, Sort sort);
- Is searching for Child entity list by Parent’s Id is bad practice because I am thinking in a RDBMS way, and not thinking in Hibernate/JPA way?