首页 > 解决方案 > Blaze Persistence Join 子查询问题

问题描述

我正在尝试通过使用 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:表 't1_' 中没有列 't1_.account_id':处理 t1_.account_id 时

标签: javajpablaze-persistence

解决方案


这可能是一个错误。为此,我打开了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);

推荐阅读