Spring Data jpa和Native查询

时间:2014-11-03 11:36:15

标签: mysql jpa spring-data-jpa

我正在做一个弹簧应用程序,并且是善良的堆栈。我运行查询如下所示

@Autowired
EntityManagerFactory entityManagerFactory;

public List countTransactionsGroupByProvider(){
    EntityManager em = entityManagerFactory.createEntityManager();
    String query = "SELECT t.order_name,count(t.order_name) as number_of_transactions from transactions_view t where "
            + "t.transaction_date between '2014-07-24' and '2014-10-27' group by t.order_name";
    List result =  em.createNativeQuery(query).getResultList();
    return result;
}

现在,这工作正常。返回以下数据:

[["Airtel",148], ["Expresso",8], ["Glo",49],
 ["MTN",110],["Select network",1],["Surfline",88],
 ["Tigo",35],["Vodafone",136],["Vouchers",30]]

我的问题是我希望以下列格式返回:

[{"order_name":"Airtel","number_of_transactions":148},
 {"order_name":"Expresso","number_of_transactions":8},
 {"order_name":"MTN","number_of_transactions":110},etc]

然后我可以将它提供给morris.js来绘制图表。 关于如何解决这个问题的任何建议。谢谢

1 个答案:

答案 0 :(得分:0)

您应该只编写一些支持代码,将数据转换为您想要的格式。不确定您是否会为了让JPA以您想要的格式生成数据而获得更大的吸引力,尽管它可以说是不可能的。