H2 issues : request works in H2 console, not in jUnit test - java
I encounter a problem with H2 Database, I can't figure out what this problem is.
I'm working on a 3 alphanums code generator that should behave by incrementing by one the highest existing code (this is a SQL function that will be stored on the DB server).
The following code works perfectly in H2 console :
SELECT TOP 1 concat(a.Chr, b.Chr, c.Chr) AS REF
FROM
(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) a(Chr)
CROSS JOIN
(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) b(Chr)
CROSS JOIN
(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) c(Chr)
WHERE concat(a.Chr,b.Chr,c.Chr) > (
SELECT TOP 1
CASE
WHEN INSTRUCTION_CODE IS NULL
THEN ''
ELSE INSTRUCTION_CODE
END
FROM ACCOUNT
ORDER BY INSTRUCTION_CODE DESC
)
ORDER BY REF;
I need to implement this request in a Java jUnit test. Here is what I did :
public static ResultSet getReference(java.sql.Connection con) throws SQLException {
String query = "SELECT TOP 1 concat(a.Chr, b.Chr, c.Chr) AS REF "
+ "FROM "
+ "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) a(Chr) "
+ "CROSS JOIN "
+ "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) b(Chr) "
+ "CROSS JOIN "
+ "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) c(Chr) "
+ "WHERE concat(a.Chr, b.Chr, c.Chr) > "
+ "(SELECT TOP 1 "
+ " CASE WHEN INSTRUCTION_CODE IS NULL "
+ " THEN '' "
+ " ELSE INSTRUCTION_CODE "
+ " END "
+ "FROM ACCOUNT order by INSTRUCTION_CODE DESC) "
+ "ORDER BY REF";
java.sql.ResultSet rs = con.createStatement().executeQuery(query);
return rs;
}
Here's the error message I get when playing it :
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT TOP 1 CONCAT(A.CHR, B.CHR, C.CHR) AS REF FROM (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))A([*]CHR) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))B(CHR) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))C(CHR) WHERE CONCAT(A.CHR, B.CHR, C.CHR) > (SELECT TOP 1 CASE WHEN INSTRUCTION_CODE IS NULL THEN '' ELSE INSTRUCTION_CODE END FROM ACCOUNT ORDER BY ACBS_PAYMENT_INSTRUCTION_CODE DESC) ORDER BY REF ";
Is there anything you see I didn't ?
Thanx
It looks like you use a recent version of H2 when you work with H2 Console, and some old version (1.4.196 or older) in your application.
Such old versions don't support the derived column list syntax. You need to use a more recent version in your application too.
Related
WITH Recursive query is not working when run using entityManager native query
I'm running this PostgreSQL query on Java and it is throwing an error "ERROR: syntax error at or near ":". But the query is working on Postgresql when I run directly. I'm thinking Array[]::integer[] is causing the issue. Can someone has any idea? String query = "WITH RECURSIVE tree AS ( SELECT id, ARRAY[]::integer[] AS ancestors \n" + " FROM regions \n" + " WHERE parent_id IS NULL\n" + " UNION ALL \n" + " SELECT soato.id, tree.ancestors || regions.parent_id \n" + " FROM regions, tree \n" + " WHERE regions.parent_id = tree.id \n" + ") \n" + " SELECT d.id FROM department d \n" + " WHERE d.region_id IN (select id from tree where 1703 = ANY(tree.ancestors))"; Query q = entityManager.createNativeQuery(query); q.getResultList();
Use an explicit cast to avoid the implicit PostgreSQL option :: for casting. ARRAY[CAST(NULL AS INTEGER)] AS ancestors
Works well in pgadmin but doesn't work in java code
with recursive Ancestor(a,d) as (select parent as a, child as d from ParentOf union select Ancestor.a , ParentOf.child as d from Ancestor, ParentOf where Ancestor.d = ParentOf.parent) Hi I ran the above code in pgadmin and it worked fine so I tried moving this to my java code the same way. But here it is called an error in the SQL syntax. What is the reason for this? I also put ; in the SQL statement, but the same error occurred. stmt.executeUpdate("with recursive " + "Ancestor(a,d) as (select parent as a, child as d from ParentOf " + "union " + "select Ancestor.a , ParentOf.child as d " + "from Ancestor, ParentOf " + "where Ancestor.d = ParentOf.parent)"); Below is the error Exception in thread "main" org.postgresql.util.PSQLException: error: syntax error, at the end of input Position: 185 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:473) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:393) at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322) at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308) at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284) at org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:258) at SqlTest3.main(SqlTest3.java:44)
You have two errors. First, to run a query, you need to use executeQuery(). Second, your SQL string in Java doesn't contain the final SELECT statement, it only contains the CTE. ResultSet rs = stmt.executeQuery( "with recursive "Ancestor(a,d) as (" + "select parent as a, child as d from ParentOf " + "union " + "select Ancestor.a ParentOf.child as d " + "from Ancestor " + " join ParentOf on Ancestor.d = ParentOf.parent " + ") " + "select * from ancestor"); // <<< this SELECT was missing I also replaced your ancient and outdated implicit join in the where clause with a "modern" (over 25 years old) explicit JOIN operator which is the recommended way to write a join these days.
Jpa native query doesnt work with group by case/when statement with binding values
Iam trying to use Jpa repository to get data from my sql server using a native query This is a simple call from my service repo.testData("h3","h3"); This is my repository query. Select statement can read the binding variable :level but group by is unable to read it. #Query(value="SELECT sum(pos.total_weekly_sales) as curr_yr_sales, sum(pos.total_weekly_qty) as curr_yr_qty, pos.vendor_nbr,pos.gmm_id,\n" + "case \n" + "when :level = 'h3' then pos.category_id\n" + "else 0\n" + "end category_id\n" + "from dbo.agg_sams_data pos\n" + "join dbo.calendar_dim cal on pos.wm_year_wk_nbr = cal.wm_year_wk_nbr\n" + "WHERE \n" + "cal.calendar_date BETWEEN '2019-09-11' and '2020-09-09'\n" + "and pos.vendor_nbr = 68494\n" + "and pos.gmm_id = 45\n" + "and (:h3Flag = 'N' or pos.category_id = 52)\n" + "GROUP by pos.vendor_nbr,pos.gmm_id,\n" + "case \n" + "when :level='h3' then pos.category_id\n" + "else 0\n" + "end",nativeQuery = true) List<List<Double>> testData(String level,String h3Flag); And i get the following error com.microsoft.sqlserver.jdbc.SQLServerException: Column 'dbo.agg_sams_data.category_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. If i pass the hardcoded value in the group by clause it works fine(as below) "GROUP by pos.vendor_nbr,pos.gmm_id,\n" + "case \n" + "when 'h3'='h3' then pos.category_id\n"
You should try putting pos.category_id into the group by instead of the whole case when statement. The problem is, that SQL Server can't be sure that the parameter in both cases will have the same value so the expressions could be different.
join fetch not working in Hibernate Query Language
I want to retrieve all the records in One database hit and for that, I am using join fetch statements below is my Query String q = "SELECT oneChat from " + Chat.class.getName() + " oneChat " + " join fetch oneChat.user1 " + " join fetch oneChat.user2 " + " join fetch oneChat.user3 " + " join fetch oneChat.groupData " + "where oneChat.dmlStatus != :dmlStatusValue" + " AND group_id = :groupIdValue" + " AND reference_id = 0" + " AND root_chat_id = oneChat.chatId"; There are total 4 foreign keys/Joins in my table so I added the join fetch statement but its not working i.e. not returning anything how ever if I remove the join fetch statements I get the result set. My Fetch on table joins is by default Eager ( didn't changed it to Lazy). Also there's no sql syntax error in the Log file. Am I missing anything ? Update: It is because the second join i.e. user2 is returning null so I wasn't getting any data. Now if anyone could tell me how can I counter this, the query should be independent it shouldn't rely on data.
IF you want to return results regardless of data being present on the dependencies, then you should use left join instead of inner join (join fetch is equal to inner join fetch): "SELECT oneChat from " + Chat.class.getName() + " oneChat " + " left join fetch oneChat.user1 " + " left join fetch oneChat.user2 " + " left join fetch oneChat.user3 " + " left join fetch oneChat.groupData " + "where oneChat.dmlStatus != :dmlStatusValue" + " AND group_id = :groupIdValue" + " AND reference_id = 0" + " AND root_chat_id = oneChat.chatId"; Now when the OneChat does not have any user2 dependency on the database, the query will still return results regardless of that. Just on the side.. if you are using prefixed, then try to add prefixes to group_id and root_chat_id fields in the where clause for clarity.
SQLite fails to find existing column in SELECT via JDBC and jOOQ
I experience some strange results working with SQLite and JDBC (via JOOQ actually, but this problem can be reproduced by executing the query string manually via JDBC). My database consists of a three tables including a many-to-many and one-to-many relationship. I try to select all values of the 'main' table and join all needed values out of the relationship tables: SELECT location.name, world.world, player.player FROM location JOIN world ON location."world-id" = world."world-id" LEFT OUTER JOIN (location2player JOIN player ON location2player."player-id" = player."player-id") ON location."location-id" = location2player."location-id" Within JDBC this query fails: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such column: player.player) When I execute the query in an external SQLite editor such as SQLite Manager for Firefox it works as expected. I work with sqlite-jdbc-3.7.2 which I cannot change. For reference, the JOOQ query is: create.select(LOCATION.NAME,WORLD.WORLD_,PLAYER.PLAYER_) .from(LOCATION .join(WORLD) .on(LOCATION.WORLD_ID.eq(WORLD.WORLD_ID) ) .leftOuterJoin(LOCATION2PLAYER .join(PLAYER) .onKey() ) .on(LOCATION.LOCATION_ID.eq(LOCATION2PLAYER.LOCATION_ID) ) .fetch() Why fails this query in JDBC and how am I supposed to fix it?
While I think that you wrote valid ANSI SQL, it may well be that SQLite interprets your statement slightly differently. But you don't really need to nest joins the way you do. Try this insted: SELECT location.name, world.world, player.player FROM location JOIN world ON location."world-id" = world."world-id" LEFT OUTER JOIN location2player ON location."location-id" = location2player."location-id" LEFT OUTER JOIN player ON location2player."player-id" = player."player-id"
I was able to recreate your issue under sqlite-jdbc-3.7.2 using sql = "SELECT location.name, " + "world.world, " + "player.player " + "FROM " + "location " + "JOIN world " + "ON location.\"world-id\" = world.\"world-id\" " + "LEFT OUTER JOIN (location2player " + "JOIN " + "player " + "ON location2player.\"player-id\" = player.\"player-id\") " + "ON location.\"location-id\" = location2player.\"location-id\""; The problem appears to be that the location2player and player tables are "hidden" inside the parentheses () of the sub-join and are unavailable to the initial column list and the final ON clause. The following statement avoids that problem by giving the subquery an alias and using the alias name in those two places: sql = "SELECT " + "location.name, " + "world.world, " + "playerlocation.player " + "FROM " + "location " + "JOIN " + "world " + "ON location.\"world-id\" = world.\"world-id\" " + "LEFT OUTER JOIN " + "( " + "SELECT location2player.\"location-id\", player.player " + "FROM " + "location2player " + "JOIN " + "player " + "ON location2player.\"player-id\" = player.\"player-id\"" + ") AS playerlocation " + "ON location.\"location-id\" = playerlocation.\"location-id\"";