我在剧中使用了一个ebean查询!框架,用于根据不同的列查找记录列表。这似乎是一个非常简单的查询,但问题是ebean方法setDistinct(true)实际上并没有将查询设置为distinct。
我的查询是:
List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findList();
在我的搜索结果中,我得到了重复的艺术家名称。
从我所看到的,我相信这是正确的语法,但我可能是错的。我很感激任何帮助。谢谢。
答案 0 :(得分:1)
根据Ebean bug跟踪器上的issue #158: Add support for using setDistinct (by excluding id property from generated sql),问题是ID列被隐式添加到select查询的开头。这使得不同的关键字在ID列上起作用,ID列始终是不同的。
这应该在Ebean 4.1.2中修复。
答案 1 :(得分:1)
我刚刚遇到同样的问题,无法弄明白。正如hfs所说,它已在以后的版本中修复,但如果你被困一段时间,你可以使用
findSet()
所以在你的例子中使用
List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findSet();
答案 2 :(得分: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();
答案 3 :(得分:0)
我有一个解决方案: -
RawSql rawSql = RawSqlBuilder .parse(&#34;选择不同的情况,当PARENT_EQUIPMENT_NUMBER为空,那么EQUIPMENT_NUMBER ELSE PARENT_EQUIPMENT_NUMBER结束为PARENT_EQUIPMENT_NUMBER&#34; + &#34; FROM TOOLS_DETAILS&#34;)。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;