【问题标题】:Ebean - Which find to use for multiple results?Ebean - 哪个查找可用于多个结果?
【发布时间】:2013-05-08 16:24:49
【问题描述】:

我正在使用 Play Framework 和 Ebean,我需要通过搜索“company_id”来查找“value”列中的信息。但是,会有多行具有相同的 company_id,我需要找到所有行并将它们放在一个列表中(不一定是列表,但最有意义)。

我的表格如下所示:

+----+------------+-----------+-----------+
| id | company_id | parent_id |   value   |
+----+------------+-----------+-----------+
|  1 |          1 |         0 | Group1    |
|  2 |          1 |         1 | SubGroup1 |
+----+------------+-----------+-----------+

我的代码是这样的:

@Entity
public class Groups extends Model {

    private static final long serialVersionUID = 1L;

    @Id
    public long id;

    public Long company_id;

    public Long parent_id;

    public String value;


    public static final Finder<Long, Groups> find = new Finder<Long,Groups>(Long.class, Groups.class);

    public static Groups findByCompanyId(Long id){
        return find.where().eq("company_id", id).findUnique();
    }

显然,.findunique();不会工作,因为它不是唯一的,我应该使用什么?我的字符串 value 是否应该保持为字符串?

【问题讨论】:

    标签: java mysql playframework playframework-2.0 ebean


    【解决方案1】:

    Ebean 提供了一个方法findList(),它返回一个List

    我认为这应该会给你想要的结果:

    public static List<Groups> findByCompanyId(Long id){
        return find.where().eq("company_id", id).findList();
    }
    

    我不确定您的 value 列的内容。如果是组的 ID,您可以将类型更改为 Groups。但这将需要 Ebean 通过其 API 不支持的递归查询,您必须为此编写原始 SQL。如果您想避免这些递归查询,您需要通过 Ebean 获取所有组并在 Java 中过滤它们。

    【讨论】:

      猜你喜欢
      • 2016-12-26
      • 2021-11-14
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      相关资源
      最近更新 更多