2016-01-29 84 views
1

嗨boyz嗨girsl我有一個問題。我有一個代碼:玩框架2.4 ebean [PersistenceException:Property []找不到models.SearchContent]

公共靜態列表createPosts(PagedList searchContents){

List<Post> posts = new ArrayList<Post>(); 
    List<SearchContent> searchContentsList = new ArrayList<SearchContent>(); 
    searchContentsList = searchContents.getList(); 

PaggedList發生器:

PagedList<SearchContent> list = Ebean.find(SearchContent.class) 
       .setRawSql(rawSql) 
       .findPagedList(pageNumber, resultsToPage); 

而且我有一個例外:

[PersistenceException: Property [] not found on models.SearchContent] 

在這line:

searchContentsList = searchContents.getList(); 

爲什麼我得到一個錯誤?

回答

2

它是因爲Ebean在select子句屬性上執行一些邏輯來獲取對象的屬性名稱。它通過刪除下劃線將其轉換爲camelCase。例如。如果我們有「從...選擇myid」,那麼它將搜索名稱爲「myid」的屬性,但在您的Model類中,您可能已將成員變量名稱寫爲「myId」。

如果您的查詢類似於「select my_id from ..」,如果您的成員變量名稱爲myid,它將正常工作。

0

我有一個解決方案: 您的屬性名稱在原始查詢和模型應該與數據庫表相同。

示例代碼: -

<code> 

RawSql rawSql = RawSqlBuilder 
       .parse("SELECT distinct CASE WHEN PARENT_EQUIPMENT_NUMBER IS NULL THEN EQUIPMENT_NUMBER ELSE PARENT_EQUIPMENT_NUMBER END AS PARENT_EQUIPMENT_NUMBER " + 
         "FROM TOOLS_DETAILS").create(); 

    Query<ToolsDetail> query = Ebean.find(ToolsDetail.class); 

    ExpressionList<ToolsDetail> expressionList = query.setRawSql(rawSql).where();//ToolsDetail.find.where(); 

    if (StringUtils.isNotBlank(sortBy)) { 
     if (StringUtils.isNotBlank(sortMode) && sortMode.equals("descending")) { 
      expressionList.setOrderBy("LPAD("+sortBy+", 20) "+"desc"); 

      //expressionList.orderBy().asc(sortBy); 
     }else if (StringUtils.isNotBlank(sortMode) && sortMode.equals("ascending")) { 

      expressionList.setOrderBy("LPAD("+sortBy+", 20) "+"asc"); 
      // expressionList.orderBy().asc(sortBy); 
     } else { 
      expressionList.setOrderBy("LPAD("+sortBy+", 20) "+"desc"); 

     } 


    } 
    if (StringUtils.isNotBlank(fullTextSearch)) { 
     fullTextSearch = fullTextSearch.replaceAll("\\*","%"); 
     expressionList.disjunction() 
       .ilike("customerSerialNumber", fullTextSearch) 
       .ilike("organizationalReference", fullTextSearch) 
       .ilike("costCentre", fullTextSearch) 
       .ilike("inventoryKey", fullTextSearch) 
       .ilike("toolType", fullTextSearch); 
    } 

    //add filters for date range 
    String fromContractStartdate = Controller.request().getQueryString("fm_contract_start_date_from"); 
    String toContractStartdate = Controller.request().getQueryString("fm_contract_start_date_to"); 
    String fromContractEndtdate = Controller.request().getQueryString("fm_contract_end_date_from"); 
    String toContractEnddate = Controller.request().getQueryString("fm_contract_end_date_to"); 

    if(StringUtils.isNotBlank(fromContractStartdate) && StringUtils.isNotBlank(toContractStartdate)) 
    { 

     Date fromSqlStartDate=new Date(AppUtils.convertStringToDate(fromContractStartdate).getTime()); 
     Date toSqlStartDate=new Date(AppUtils.convertStringToDate(toContractStartdate).getTime()); 
     expressionList.between("fmContractStartDate",fromSqlStartDate,toSqlStartDate); 
    }if(StringUtils.isNotBlank(fromContractEndtdate) && StringUtils.isNotBlank(toContractEnddate)) 
    { 
     Date fromSqlEndDate=new Date(AppUtils.convertStringToDate(fromContractEndtdate).getTime()); 
     Date toSqlEndDate=new Date(AppUtils.convertStringToDate(toContractEnddate).getTime()); 
     expressionList.between("fmContractEndDate",fromSqlEndDate,toSqlEndDate); 
    } 

    PagedList pagedList = ToolsQueryFilter.getFilter().applyFilters(expressionList).findPagedList(pageNo-1, pageSize); 

    ToolsListCount toolsListCount = new ToolsListCount(); 
    toolsListCount.setList(pagedList.getList()); 
    toolsListCount.setCount(pagedList.getTotalRowCount()); 
    return toolsListCount;