와 JPQL 쿼리를 작성하는?어떻게`COUNT` 및 <code>COUNT</code> 및 <code>GROUP BY</code>로 <strong>JPQL</strong> 쿼리를 작성하고 <code>Map<Integer,Integer></code> 같은 결과를 얻을 방법`GROUP BY`
public class CommentEntity {
private int id;
private int parentId;
private EntityParentType parentType;
private Long replyCounts;
private String author;
private String comment;
}
. 쿼리가 실패입니다
SELECT parent_id, COUNT(*) FROM comment AS c WHERE c.parent_type = 2 AND c.parent_id IN (64,65) GROUP BY parent_id
하지만 JPQL :
@Repository
@Transactional(readOnly = true)
public interface CommentRepository extends JpaRepository<CommentEntity, Integer> {
@Query(value = "SELECT c.parentId, COUNT(c.id) FROM CommentEntity AS c WHERE c.parentType = ?1 AND c.parentId IN (?2) GROUP BY c.parentId")
Map<Integer, Integer> findReplyCountByParentIds(EntityParentType entityParentType, List<Integer> ids);
}
public enum EntityParentType {
PERSON,
EVENT,
COMMENT;
}
나는 MySQL을 쿼리 및 미세이 작품을 썼다. @Query(value = "SELECT c.parentId, COUNT (c.id) FROM CommentEntity AS c WHERE c.parentType = ?1 AND c.parentId IN (?2) GROUP BY c.parentId")
List<Map<Integer, Integer>> findReplyCountByParentIds(EntityParentType entityParentType, List<Integer> ids);
을 아래
Method threw 'org.springframework.dao.IncorrectResultSizeDataAccessException' exception.
result returns more than one elements
도 실패합니다. Method threw 'org.springframework.dao.InvalidDataAccessApiUsageException' exception.
No aliases found in result tuple! Make sure your query defines aliases!
나는 CommentEntity에 pacakge
를 추가 시도도
예외에 따라 여러 결과 (목록)가 있습니다. 그러나 선언 한 메소드에는지도 인스턴스가 하나만 있습니다. – pvpkiran
목록