Ebean Finder 以一种奇怪的方式加入@OneToMany 字段(4 个结果而不是 2 个) [英] Ebean Finder joins @OneToMany fields in a strange way (4 results instead of 2)

查看:35
本文介绍了Ebean Finder 以一种奇怪的方式加入@OneToMany 字段(4 个结果而不是 2 个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型类中有一个方法可以在 Finder 的帮助下查询数据库:

I have a method in my Model class which queries DB with the help of Finder:

public static List<Bet> getBetsByUser(User user){
    System.out.println("ROW COUNT: "+ find.fetch("user")
            .fetch("moneyPoolEntity.account")
            .fetch("coupon.moneyPoolEntities.account")
            .fetch("moneyPoolEntity.bettingTable")
            .fetch("moneyPoolEntity.moneyPoolType.currency").setDistinct(true)
            .where().eq("user", user).findRowCount() );
    List<Bet> betList = find.fetch("user")
            .fetch("moneyPoolEntity.account")
            .fetch("coupon.moneyPoolEntities.account")
            .fetch("moneyPoolEntity.bettingTable")
            .fetch("moneyPoolEntity.moneyPoolType.currency").setDistinct(true)
            .where().eq("user", user).findList();
    System.out.println("LIST COUNT: "+betList.size());
    for(Bet b : betList){
        System.out.println(b.id+" b.coupon.moneyPoolEntities.size() = " + b.coupon.moneyPoolEntities.size());
        for (MoneyPoolEntity mpe : b.coupon.moneyPoolEntities){
            System.out.println("mpe--"+mpe.id+" ; account: "+mpe.account.id);
        }
    }
    return betList;
}

用于说明每个 coupon.moneyPoolEntities 列表中有 3 个实体.所以,如果输出的大小为 1,一切都很好,我有:.findRowCount() = 1betList.size() = 1而 .coupon.moneyPoolEntities.size() = 3和手动 SQL 查询返回表:具有除 MoneyPoolEntity.idAccount.id 之外的所有相同字段.因此 Ebean 能够将 3 个实体打包到 .coupon.moneyPoolEntities 列表中.

Useful to say that there is 3 entities in each coupon.moneyPoolEntities list. So, in case size of the output is 1, everything is fine and I have: .findRowCount() = 1 betList.size() = 1 while .coupon.moneyPoolEntities.size() = 3 and manual SQL querying returns table: with all equal fields except MoneyPoolEntity.id and Account.id. So Ebean is able to pack 3 entities into .coupon.moneyPoolEntities list.

PrintLn 结果如下:

PrintLn result looks like:

ROW COUNT: 1 
LIST COUNT: 1
243 b.coupon.moneyPoolEntities.size() = 3
mpe--201 ; account: 241
mpe--203 ; account: 243
mpe--202 ; account: 242
bets.size() = 1

当我在 coupon.moneyPoolEntities 列表中添加一个仍然有 3 个实体的 Bet 实体时,问题就出现了.

The problem arrives when I add one more Bet entity still with 3 entities in coupon.moneyPoolEntities list.

SQL 查询结果如下:

SQL query result looks like:

但现在不是两个 Bet 实体,每个实体都有 coupon.moneyPoolEntities 的大小为 3,Ebean 以一种非常奇怪的方式打包:

But now instead of two Bet entities each with coupon.moneyPoolEntities of size 3 Ebean packs it in a very strange way:

PrintLn 结果:

PrintLn result:

ROW COUNT: 2
LIST COUNT: 4
261 b.coupon.moneyPoolEntities.size() = 2
mpe--202 ; account: 242
mpe--203 ; account: 243
243 b.coupon.moneyPoolEntities.size() = 2
mpe--202 ; account: 242
mpe--203 ; account: 243
261 b.coupon.moneyPoolEntities.size() = 2
mpe--202 ; account: 242
mpe--203 ; account: 243
243 b.coupon.moneyPoolEntities.size() = 2
mpe--202 ; account: 242
mpe--203 ; account: 243
bets.size() = 4

为什么?

推荐答案

我已经解决了这个问题.出于某种原因,PostgresDB 有时会返回已排序的表,有时不会(尽管我没有要求对其进行排序).

I've solved the problem. For some reason PostgresDB was sometimes returning sorted table and sometimes not (although I haven't asked to sort it).

问题是,在我的例子中,Ebean 只有在表格已排序时才能正确连接.

The problem is that Ebean in my case joins correctly only if the table is sorted.

因此,对我有用的解决方法是将 .orderBy("id") 添加到提取序列中.

So, the workaround that works for me is the addition of .orderBy("id") to the fetch sequence.

这篇关于Ebean Finder 以一种奇怪的方式加入@OneToMany 字段(4 个结果而不是 2 个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆