【问题标题】:ebean order by with oneToMany property with paginationebean order by with oneToMany property with pagination
【发布时间】:2017-05-22 12:42:32
【问题描述】:

我正在尝试获取分页订单,按当前版本的创建日期排序 (currentVersion = true)

这里是 Order 类:

@Entity
public class Offer extends Model {
    @Id   
    public Long id;
    ....
    @OneToMany(mappedBy = "offer", fetch = FetchType.EAGER)
    public List<Version> versions; 
    ...  
}

这里是版本类

@Entity
public class Version extends Model {

    @Id   
    public Long id;
    @ManyToOne
    @JoinColumn(name = "OFFER_ID")
    public Offer offer;
    public Boolean currentVersion = false;
    public Date creation;
    ...  
}

这是我在 eBean 文档中找到的获取代码:

 Query<Offre> query = Ebean.find(Offer.class);

 List<Offer> offers = query.fetch("versions")
                               .where()
                               .eq("versions.currentVersion", true)
                               .orderBy("versions.creation desc nulls last")
                               .setFirstRow(0)
                               .setMaxRows(10)
                               .findPagedList().getList();

这是预期的 SQL

SELECT * FROM
  (SELECT     /*+ FIRST_ROWS(10) */     rownum rn_,  a.*  FROM
    (SELECT t0.id c0,
       ... 
      t0.OPTLOCK c32
    FROM offer t0
    INNER JOIN version t1 on t0.id = t1.OFFER_ID 
    ORDER BY t1.creation
    ) a
  WHERE rownum <= 10
  ) ; --bind()

这是实际的 SQL

First one:

SELECT *
FROM
  (SELECT
    /*+ FIRST_ROWS(10) */
    rownum rn_,
    a.*
  FROM
    ( SELECT DISTINCT t0.id c0,
      ...
      t0.id
      ...
    FROM offer t0
    JOIN versions u1  ON u1.OFFER_ID = t0.id
    WHERE u1.current_version = true
    AND u1.current_version   = true
    ORDER BY t0.id
    ) a
  WHERE rownum <= 10
  ) ; 

Second one : 

SELECT t0.OFFER_ID c0,
  t0.id c1,
  ...
  t0.creation c3,
  ...
  t0.current_version c12,
  ...
  t0.OFFER_ID c30,
  ...
FROM versions t0
WHERE (t0.OFFER_ID) IN (990,991,992,993,994,995,996,997,998,999)
ORDER BY t0.creation; 

问题:我需要更改哪些内容才能获得预期的 SQL?

非常感谢您的帮助

使用的版本: 来自 play 2.5.14 的 ebean 7.6.1

【问题讨论】:

  • 从 Play 2.4 迁移到 Play 2.5 后出现的问题。在以前的版本中,设置 setFirstRow(0) / setMaxRows(10) 并不意味着自动有 order by t0.id 子句。我可以做一个原始的 SQL 命令。现在它不再起作用了。

标签: pagination sql-order-by one-to-many ebean


【解决方案1】:

我找到了一个走动:

代替

List<Offer> offers = query.fetch("versions")
                               .where()
                               .eq("versions.currentVersion", true)
                               .orderBy("versions.creation desc nulls last")
                               .setFirstRow(0)
                               .setMaxRows(10)
                               .findPagedList().getList();

我找到了:

List<Offer> offers = query.fetch("versions")
                               .where()
                               .eq("versions.currentVersion", true)
                               .orderBy("u1.creation desc") // <-- line with the change
                               .setFirstRow(0)
                               .setMaxRows(10)
                               .findPagedList().getList();

我确保报价和版本之间的连接只进行一次,然后假设版本连接别名为 u1。

至少,它完成了这项工作。

再见

【讨论】:

    猜你喜欢
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 2012-12-15
    • 2011-04-10
    • 1970-01-01
    相关资源
    最近更新 更多