简体   繁体   中英

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.

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> rows = crit.list();
for(Object r: rows){
  Object[] row = (Object[]) r;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM