[英]HIbernate: select multiple distinct columns resulting in error
I am trying to perform a query 我正在尝试执行查询
Hibernate: select distinct this_.platform as y0_, this_.device as y1_, this_.date as y2_ from my_lab this_ where this_.brand=? and this_.network=?
using the code: 使用代码:
Criteria crit = entityManager.unwrap(Session.class).createCriteria(Lab.class);
ProjectionList projList = Projections.projectionList();
if (platform == null) {
projList.add(Projections.property("platform"));
} else {
crit.add(Restrictions.eq("platform", platform));
}
if (device == null) {
projList.add(Projections.property("device"));
} else {
crit.add(Restrictions.eq("device", device));
}
if (date == null) {
projList.add(Projections.property("date"));
} else {
crit.add(Restrictions.eq("date", dateOfVideo));
}
if (brand == null) {
projList.add(Projections.property("brand"));
} else {
crit.add(Restrictions.eq("brand", brand));
}
if (network == null) {
projList.add(Projections.property("network"));
} else {
crit.add(Restrictions.eq("network", network));
}
crit.setProjection(Projections.distinct(projList));
List<String> list = crit.list();
return list;
But when I call this service, it gives me following error: 但是当我调用此服务时,它给了我以下错误:
"Could not write content: [Ljava.lang.Object; cannot be cast to java.lang.String; nested exception is com.fasterxml.jackson.databind.JsonMappingException: [Ljava.lang.Object; cannot be cast to java.lang.String",
Unable to understand why. 无法理解原因。 Have mentioned produces={MediaType.APPLICATION_JSON_VALUE}
in the get request as well. 在get请求中也提到了produces={MediaType.APPLICATION_JSON_VALUE}
。
I think the problem is the line 我认为问题是线
List<String> list = crit.list();
Actually with multiple projections you should expect List<Object>
and access the values like this 实际上,对于多个投影,您应该期望List<Object>
并访问像这样的值
List<Object> rows = crit.list();
for(Object r: rows){
Object[] row = (Object[]) r;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.