8

我在剧中使用ebean查询!框架来查找基于不同列的记录列表。这似乎是一个非常简单的查询,但问题是 ebean 方法 setDistinct(true) 实际上并没有将查询设置为不同的。

我的查询是:

List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findList();

在我的结果中,我得到重复的艺术家姓名。

从我所见,我相信这是正确的语法,但我可能是错的。我会很感激任何帮助。谢谢你。

4

4 回答 4

2

我只是突然遇到了同样的问题,无法弄清楚。正如 hfs 所说,它已在更高版本中得到修复,但如果你被卡住了一段时间,你可以使用

findSet()

所以在你的例子中使用

List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findSet();
于 2015-11-17T13:33:08.137 回答
1

根据问题 #158:在 Ebean 错误跟踪器上添加对使用 setDistinct(通过从生成的 sql 中排除 id 属性)的支持,问题是在选择查询的开头隐式添加了一个 ID 列。这使得 distinct 关键字作用于 ID 列,这将始终是不同的。

这应该在 Ebean 4.1.2 中修复。

于 2014-10-01T10:18:07.037 回答
0

我有一个解决方案:-

RawSql rawSql = RawSqlBuilder .parse("SELECT distinct CASE WHEN PARENT_EQUIPMENT_NUMBER 为 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;
于 2017-08-29T12:20:25.817 回答
0

作为替代方案,您可以使用本机 SQL 查询 (SqlQuery)。该机制在这里描述: https ://ebean-orm.github.io/apidocs/com/avaje/ebean/SqlQuery.html

这是来自文档:

public interface SqlQuery
extends Serializable
Query object for performing native SQL queries that return SqlRow's.
Firstly note that you can use your own sql queries with entity beans by using the SqlSelect annotation. This should be your first approach when wanting to use your own SQL queries.

If ORM Mapping is too tight and constraining for your problem then SqlQuery could be a good approach.

The returned SqlRow objects are similar to a LinkedHashMap with some type conversion support added.

// its typically a good idea to use a named query
// and put the sql in the orm.xml instead of in your code

    String sql = "select id, name from customer where name like :name and status_code = :status";

    SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
    sqlQuery.setParameter("name", "Acme%");
    sqlQuery.setParameter("status", "ACTIVE");

    // execute the query returning a List of MapBean objects
    List<SqlRow> list = sqlQuery.findList();
于 2015-11-27T01:15:56.033 回答