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

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

问题描述

我在我的模型类中的方法,该方法查询数据库与搜索的帮助:

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()= 1
betList.size()= 1
而.coupon.moneyPoolEntities.size()= 3
和手动SQL查询返回表:

与除 MoneyPoolEntity.id人人平等领域 Account.id 。所以Ebean能够Pack 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个实体问题到达。

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

SQL查询的结果是这样的:

SQL query result looks like:

但现在,而不是两个投注实体每个 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搜索中加入一种奇怪的方式@OneToMany字段(4结果而不是2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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