0

I am trying to do a join on subquery by using blaze bit persistence, I have a criteria builder like below,

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);

when I execute it I am getting sql query like below

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 

The problem occurs in the on clause, when i try to compare the two table account_id in the on cluase I am getting column missing error since the alias is formed for the account_id defaultly in the subquery. Please let me know how to resolve it. I am using the hibernate5.4 JPA vendor.

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

Arun Prasat
  • 340
  • 1
  • 9

1 Answers1

0

This might be a bug. I opened https://github.com/Blazebit/blaze-persistence/issues/1285 for this purpose. Could you maybe tell me which Hibernate version you are using and if the SQL query works if you add a closing parenthesis before the WHERE clause? It seems that parenthesis is missing here, but I don't know yet why.

UPDATE:

Try this as a workaround:

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);
Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
  • I am using Hibernate - 5.4. I have updated the query the issue is not with parenthesis. while doing join on subquery, on clause not able to find the column name of the subquery since it is having an alias. ex: There's no column 't1_.account_id' in table 't1_' if I change this the on clause to -- s0_.account_id = t1_. col_0_0_ it is working – Arun Prasat Apr 02 '21 at 08:43
  • Which database are you using? This could be an issue with that particular database. You could workaround this by registering the subquery as CTE instead. – Christian Beikov Apr 02 '21 at 10:10
  • I am using the clickhouse database which uses mysql queries, If you check the above query I am using a CTE to bind parameters but still am facing the same issue. – Arun Prasat Apr 04 '21 at 03:09
  • I updated my answer with a possible workaround. – Christian Beikov Apr 06 '21 at 07:56
  • It didnt help, I am getting the below issue - Unsupported SQL of `with aCTE(account_id, id) – Arun Prasat Apr 08 '21 at 05:12
  • That's a pity, as that means you are blocked by the Github issue I created for this. I'll see what I can do. I wanted to do a new release soon anyway. I'll let you know when I published it. – Christian Beikov Apr 08 '21 at 12:51
  • wow great to hear it thank you so much. I have one more doubt if I create a inner join on subquery in MySQL, why by defaultly the dual table is appended. (ex : select null account_id,null id from dual where 1=0 union all ) In the new release is it handled? – Arun Prasat Apr 09 '21 at 09:07
  • This is done because MySQL does not support re-aliasing of columns at the table reference alias site. To avoid having to rewrite the subquery SQL (which is generated by Hibernate) we prepend the union part you are seeing for naming the select items. – Christian Beikov Apr 09 '21 at 11:23
  • Hi, I tried to reproduce the issue so I can fix it, but couldn't. In my tests, the _select union_ part is always properly rendered. Can you help me figure out what I did wrong. See here https://github.com/Blazebit/blaze-persistence/pull/1293 – Christian Beikov Apr 19 '21 at 11:32
  • Use MySql as configured DBMS dialect. – Arun Prasat Apr 20 '21 at 06:38
  • Which Blaze-Persistence version are you using? I tried that configuration, but it works for me. – Christian Beikov Apr 20 '21 at 07:49
  • I am using - 1.5.1. you are checking the query returned from cb.getQueryString() which is not completely parsed. Please try to get the resultset and check the mysql db for the executed query in log. with that query you can reproduce the same issue – Arun Prasat Apr 20 '21 at 10:15
  • Could you try with 1.6.0-Alpha2 please? I think this was solved already. – Christian Beikov Apr 20 '21 at 14:30
  • Hey Arun, I just released 1.6.0 and I hope it fixes your issue already. Could you please check and let me know? – Christian Beikov Apr 25 '21 at 08:27
  • Does it work for you? I would really like to close the issue as I can't reproduce your problem. – Christian Beikov May 16 '21 at 09:30
  • It dint help I used a custom query builder. Please close the ticket, Thank you so much for the help. – Arun Prasat May 21 '21 at 04:13
  • Too bad, I really wanted to make this work for you, but I simply can't reproduce your issue :/ – Christian Beikov May 21 '21 at 09:10