【问题标题】:JPA Data Repository result losing its data typeJPA Data Repository 结果丢失其数据类型
【发布时间】:2012-08-31 07:03:06
【问题描述】:

我正在尝试获取以下代码来显示我的查询结果。不幸的是,它不起作用。

@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);

在我的服务层我做...

@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 在变量调试器中最终成为Object[]。我不确定为什么它不遵守我告诉它使用的Map&lt;String, Integer&gt;

我想作为替代方案,我可以只返回一个对象列表。我试图在谷歌周围试图找出要搜索哪些关键字才能找到这样的结果。虽然理想情况下,如果它只返回 Map!

,我希望避免仅为此查询结果创建一个对象

【问题讨论】:

  • 为什么你认为Spring Data可以将Object[]隐式转换为Map?它是否记录在某处?
  • 我能够在10.4.1.2 部分找到docs.jboss.org/hibernate/core/3.3/reference/en/html/…,但我仍然不明白为什么它无法处理Map&lt;String, Integer&gt;。我正在研究那个例子,试图让它为我工作。

标签: java spring-data-jpa


【解决方案1】:

将我的查询结果转换为 Object[] 并使用此处的文档:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-querying-executing 10.4.1.2 部分解决了我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2012-06-18
    • 2014-08-31
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多