【发布时间】:2016-01-07 09:17:50
【问题描述】:
我有以下结构:
public class ConversationModel extends Model {
@Id
public Long id;
@OneToMany(mappedBy="conversation", cascade= CascadeType.ALL)
public List<EventModel> events;
}
和
public abstract class EventModel extends Model {
@Id
public Long id;
public Date time;
}
现在,当我从对话模型中获取页面时,我想按数组事件中 EventModel 的最新条目对它们进行排序。
我尝试了类似的方法:
return find.where().disjunction()
.add(Expr.ilike("name", "%" + filter + "%"))
...
.orderBy("events.time desc, " + sortBy + " " + order)
.findPagedList(page, pageSize);
但这会以某种方式在每个事件的分页列表中返回一个 ConversationModel 条目。 (例如,如果它有 3 个事件,则 3 次相同的对话) 如果我在 orderBy 查询中省略“events desc”,我会得到想要的结果,但排序不正确。
我也尝试在查询中设置.setDistinct(true),但返回“无效的 SQL 语法”。
如何将我的所有对话归类到他们最新的活动条目?
感谢您的帮助。
编辑:
我已经设置了一个示例项目,它显示了问题:
https://github.com/dominik-ziegler/play-page-sort
第一次启动时,应用程序会将对话打印到控制台:
[debug] - application - Conversation: First Conversation; ID1
[debug] - application - Conversation Last Message: Tue Jan 12 23:03:55 CET 2016
[debug] - application - ----------
[debug] - application - ----------
[debug] - application - Conversation: Second Conversation; ID2
[debug] - application - Conversation Last Message: Tue Jan 12 23:03:55 CET 2016
[debug] - application - ----------
[debug] - application - ----------
[debug] - application - Conversation: Third Conversation; ID3
[debug] - application - Conversation Last Message: Tue Jan 12 23:03:55 CET 2016
[debug] - application - ----------
但是根据插入日期,顺序应该是:
- 第二次对话
- 第三次对话
- 第一次对话
【问题讨论】:
标签: playframework ebean playframework-2.4