gpt4 book ai didi

java - 按 ORMLite 中外部实体的属性排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:48 24 4
gpt4 key购买 nike

我如何在 ORMLite 中构建查询,以便我可以使用 orderBy 函数(使用带有原始字符串的函数或参数化的函数)引用与我正在构建的 dao 中的实体不同的实体的属性查询来自?我的查询是这样构建的:

// Inner query for performances
QueryBuilder<Performance, String> performancesQB = performanceDao.queryBuilder();
performancesQB.selectColumns("performance_id");
SelectArg performanceSelectArg = new SelectArg();
performancesQB.where().lt("date", performanceSelectArg);

// Outer query for Order objects, where the id matches in the performance_id
// from the inner query
QueryBuilder<Order, String> ordersQB = orderDao.queryBuilder();
ordersQB.where().isNull("user_id").and().in("performance_id", performancesQB);
ordersQB.orderByRaw("performances.date DESC");
pastOrdersQuery = ordersQB.prepare();

每当我尝试执行此查询时遇到的异常是:

android.database.sqlite.SQLiteException: no such column: performances.date:,
while compiling: SELECT * FROM `orders` WHERE
(`user_id` IS NULL AND `performance_id` IN
(SELECT `performance_id` FROM `performances` WHERE `date` < ? ) )
ORDER BY performances.date DESC

我在这里看到的唯一解决方案是使用 JOIN 而不是嵌套选择自己编写原始查询。这可能是一个好的解决方案吗?

最佳答案

ORMLite 现在支持简单的 JOIN 查询。这里是关于这个主题的文档:

http://ormlite.com/docs/join-queries

所以您的查询现在看起来像这样:

QueryBuilder<Performance, String> performancesQB = performanceDao.queryBuilder();
SelectArg performanceSelectArg = new SelectArg();
performancesQB.where().lt("date", performanceSelectArg);
performancesQB.orderBy("date", false);

// query for Order objects, where the id matches
QueryBuilder<Order, String> ordersQB = orderDao.queryBuilder();
ordersQB.join(performancesQB).where().isNull("user_id");
pastOrdersQuery = ordersQB.prepare();

关于java - 按 ORMLite 中外部实体的属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11079585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com