【问题标题】:Blaze Persistence Join on subquery issueBlaze Persistence Join 子查询问题
【发布时间】:2021-03-22 10:54:33
【问题描述】:

我正在尝试通过使用 blaze 位持久性来连接子查询,我有一个像下面这样的标准构建器,

cbf.create(em, b.class, "foo")
   .innerJoinOnSubquery(aCTE.class, "maxEnt")
   .from(a.class, "subFoo")
   .bind("account_id").select("subFoo.accountId","account_id")
   .bind("id").select("subFoo.id","id")
   .where("subFoo.accountId").eq(100L)
   .end()
   .on("maxEnt.account_id").eqExpression("foo.accountId")
   .end()
   .where("foo.accountId").eq(100L);

当我执行它时,我得到如下 sql 查询

SELECT s0_.id FROM   b s0_ 
   INNER JOIN (SELECT t0_.account_id     AS col_0_0_, 
                      t0_.id AS col_1_0_ 
               FROM   a t0_ 
               WHERE  t0_.account_id = 100 ) t1_ 
           ON ( ( NULL IS NULL ) 
                AND s0_.account_id = t1_.account_id) WHERE  s0_.account_id = 100 

问题出现在 on 子句中,当我尝试比较 on 子句中的两个表 account_id 时,由于别名是为account_id 默认在子查询中。请让我知道如何解决它。我使用的是 hibernate5.4 JPA 供应商。

问题:DB::Exception: There's no column 't1_.account_id' in table 't1_': While processing t1_.account_id.

【问题讨论】:

    标签: java jpa blaze-persistence


    【解决方案1】:

    这可能是一个错误。我为此打开了https://github.com/Blazebit/blaze-persistence/issues/1285。您能否告诉我您正在使用哪个 Hibernate 版本,如果您在 WHERE 子句之前添加右括号,SQL 查询是否有效?这里似乎缺少括号,但我还不知道为什么。

    更新:

    试试这个解决方法:

    cbf.create(em, b.class, "foo")
       .with(aCTE.class) // Maybe pass false as second argument to avoid inlining
         .from(a.class, "subFoo")
         .bind("account_id").select("subFoo.accountId","account_id")
         .bind("id").select("subFoo.id","id")
         .where("subFoo.accountId").eq(100L)
       .end()
       .innerJoinOn(aCTE.class, "maxEnt")
         .on("maxEnt.account_id").eqExpression("foo.accountId")
       .end()
       .where("foo.accountId").eq(100L);
    

    【讨论】:

    • 我正在使用 Hibernate - 5.4。我已经更新了问题不是括号的查询。在对子查询进行联接时,on 子句无法找到子查询的列名,因为它具有别名。例如:如果我将 on 子句更改为 -- s0_.account_id = t1_,则表 't1_' 中没有列 't1_.account_id'。 col_0_0_ 它正在工作
    • 您使用的是哪个数据库?这可能是该特定数据库的问题。您可以通过将子查询注册为 CTE 来解决此问题。
    • 我正在使用使用 mysql 查询的 clickhouse 数据库,如果您检查上述查询,我​​正在使用 CTE 绑定参数,但仍然面临同样的问题。
    • 我用可能的解决方法更新了我的答案。
    • 它没有帮助,我遇到了以下问题 - `with aCTE(account_id, id) 的 SQL 不受支持
    猜你喜欢
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多