I’m trying to get the below code to show my query results. Unfortunately it’s not working.
@Transactional
public interface ContentRepository extends JpaRepository<Content,Integer>{
@Query(nativeQuery=true, value="SELECT content_type, COUNT(*) FROM dbo.content WHERE status_id = :statusId GROUP BY content_type")
List<Map<String, Integer>> getContentCountByType(@Param("statusId")Short statusId);
On my service layer I do…
@Service
public class ContentService {
@Transactional
public Map<ContentType, Integer> getContentCountByType() {
List<Map<String, Integer>> rawContentCount = contentRepository.getContentCountByType(Status.DRAFT);
Map<ContentType, Integer> contentCount = new HashMap<ContentType, Integer>();
Map<String, Integer> objects = rawContentCount.get(0);
objects ends up being Object[] in the variable debugger. I’m unsure why it’s not obeying the Map<String, Integer> that I’ve told it to use.
I was thinking as an alternative I could just return a list of objects. I’m trying to Google around trying to figure out what keywords to search for to find such a result. Though ideally I’d like to avoid having to create an object just for this query result if it would just return Map!
Casting my query results as
Object[]and using the documentation here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-querying-executing at section10.4.1.2solved my issue