play framework 2.4 ebean [PersistenceException:在[model.SearchContent]上找不到Property []

时间:2016-01-29 18:25:28

标签: java playframework ebean

嗨boyz hi girsl我有一个问题。我有一个代码:

public static List 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]

在这一行:

    searchContentsList = searchContents.getList();

为什么会出错?

2 个答案:

答案 0 :(得分:2)

因为Ebean在select子句属性上执行一些逻辑以获取对象的属性名称。它通过删除下划线将其转换为camelCase。例如。如果我们从...中选择了#id;&#34;然后它将搜索名称为#34; myid&#34;但在你的Model类中,你可能已经将成员变量名称写成&#34; myId&#34;。

如果您的查询类似于&#34;从...中选择my_id,它将正常工作。如果你的成员变量名是myid。

答案 1 :(得分:0)

我有一个解决方案: 原始查询中的属性名称和Model应与数据库表相同。

示例代码: -

<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;