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

标签 java android foreign-keys sql-order-by ormlite

我如何在 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/

相关文章:

android - 9-patch 图像在 TextView 周围绘制 1 个像素的边框

android - 将值插入 SQLite 数据库

mysql - 从外部表中选择值

python - SQLAlchemy 表之间的多种关系

java - 为什么我不在类路径中使用通配符?

android - 无法构建创建的 Titanium Android 模块

java - 具有任意 AND 子句的动态 spring 数据 jpa 存储库查询

mysql - SQL : Add A Foreign Key relation to two existing(filled) tables

java - Spring Webflow适合异步吗?流量?

java - session 上下文为空 JBOSS 7.1