簡體   English   中英

HIbernate:選擇多個不同的列會導致錯誤

[英]HIbernate: select multiple distinct columns resulting in error

我正在嘗試執行查詢

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=?

使用代碼:

    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;

但是當我調用此服務時,它給了我以下錯誤:

 "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",

無法理解原因。 在get請求中也提到了produces={MediaType.APPLICATION_JSON_VALUE}

我認為問題是線

List<String> list = crit.list();

實際上,對於多個投影,您應該期望List<Object>並訪問像這樣的值

List<Object> rows = crit.list();
for(Object r: rows){
  Object[] row = (Object[]) r;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM