【发布时间】:2017-08-25 03:10:37
【问题描述】:
我想问一些问题,如何从列表中获取特定值并添加到另一个列表,假设我有列表(这是来自 hibernate DAO 的 cust 列表),例如:
[["marketplace","001-002-003"],["insurance","142-523-132"],["car purchase","982349824"]]
我只想从该列表中获取“marketplace”、“insurance”和“car purchase”的值并添加到名为“bu”的新列表中
这是我的代码
public @ResponseBody String findBU(@RequestBody AllCustomerHist customer){
BigDecimal id= customer.getId();
String message;
List<String> bu= new ArrayList<>();
int i;
System.out.println("ID = "+id);
List<AllCustomerHist> cust = allCustomerHistService.findBU(id);
for (i=0; i<cust.size(); i++){
System.out.println("iteration = "+i);
// stumbled here //
}
JSONObject json = new JSONObject();
json.put("id", id);
json.put("BU", bu);
message = json.toString();
return message;
}
这是我的 AllCustomerHistDaoImpl 类
//release 1.3
@SuppressWarnings("unchecked")
public List<AllCustomerHist> findBU(BigDecimal adpId) {
// TODO cek kodingan
Criteria criteria = getSession().createCriteria(AllCustomerHist.class)
.setProjection(Projections.projectionList()
.add(Projections.property("srctable"), "srctable")
.add(Projections.property("customerId"), "customerId"))
.add(Restrictions.eq("adpId", adpId));
return (List<AllCustomerHist>)criteria.list();
}
注意AllCustomerHist是一个实体类,用于在hibernate中定义表
感谢您的帮助:D
【问题讨论】:
-
AllCustomerHist的结构是什么?更重要的是,为什么假设我们会知道? -
为什么使用 ID 作为 BigDecimal?
-
您已经以一种看起来像地图的方式显示了来自 Hibernate 的客户列表。然后,您将使用带有 AllCustomerHist 类型元素的列表。好像不兼容。
-
@PauloPedroso cust 的结果不是一对 key:value,它的结果集来自 2 列,首先是 srctable 列(“市场、保险、购车”来自这里)和第二个来自 customerID 列
-
@galih 也许你应该发布 AllCustomerHist 代码。
标签: java spring list hibernate rest