【问题标题】:Ebean query; using an object in the WHERE clauseEbean查询;在 WHERE 子句中使用对象
【发布时间】:2014-04-22 13:27:18
【问题描述】:

我有一个 StockItem 类,它的字段是 Warehouse 对象。我想运行 Ebean 查询以查找某个仓库中的所有 StockItem:

List<StockItem> result = StockItem.find()
                                  .where()
                                  .eq("warehouse", warehouse)
                                  .findList();

但是,结果总是大小为 0,即使在给定 Warehouse 的数据库中有 StockItems 时也是如此。我在 Warehouse 类中覆盖了 equals() 。

如果我查询所有 StockItems 的列表,然后测试仓库字段的相等性,我可以找到我正在寻找的仓库:

String resultString;
List<StockItem> result = StockItem.find().findList();
if(result.get(0).warehouse.equals(warehouse)) {
        resultString = "success";
}

这是唯一的方法吗?

编辑:这是我在仓库中的 equals()

@Override
public boolean equals(Object o) {
    if(o instanceof Warehouse) {
        Warehouse other = (Warehouse)o;
        return name.equals(other.name) && address.equals(other.address);
    } else {
        return false;
    }
}

【问题讨论】:

  • 请向您展示仓库中已覆盖的等号
  • 您应该启用 Ebean 的语句日志记录来调试您在 DB GUI 中的问题

标签: java sql ebean


【解决方案1】:

我不知道ebean 是什么,但你可以试试这样吗?

List<StockItem> result = StockItem.find()
                                  .where()
                                  .eq("warehouse.id", warehouse.getId())
                                  .findList();

【讨论】:

  • 这行不通,因为在前面几行中,我创建了一个名为“warehouse”的新 Warehouse 对象用于查询;它不会有一个ID。我是否应该先查询以找到正确的 Warehouse id,然后在 StockItem 查询中使用它?
  • 我现在能够按名称成功查询仓库以找到正确的 id 并在 StockItem 查询中使用它并检索 StockItems 列表
猜你喜欢
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 2021-11-16
  • 2017-01-05
  • 2015-02-18
相关资源
最近更新 更多