How to write Date range query in H2 using jooq - java

I'm using mybatis library to execute native sql queries.
I'm using jooq to generate dynamic sql queries for different dialects.
Syntax:
condition = condition.and(DSL.field("START_TIME_").between("2014-10-12")
.and("2014-10-12"));
Error:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM ACT_HI_PROCINST WHERE (DELETE_REASON_ IS NULL AND START_TIME_ BETWEEN CAST(:[*]1 AS VARCHAR) AND CAST(:2 AS VARCHAR))
LIMIT ? OFFSET ? "; expected "NOT, EXISTS, INTERSECTS, SELECT, FROM"; SQL statement:
select *
from ACT_HI_PROCINST
where (DELETE_REASON_ is null and START_TIME_ between cast(:1 as varchar) and cast(:2 as varchar))

Related

Unknown column sql error while using jooq

Query:
this.dslContext.select(
ROLE.asterisk(),
multiset(
select(PERMISSION.asterisk())
.from(ROLE_PERMISSION)
.innerJoin(PERMISSION)
.on(ROLE_PERMISSION.PERMISSION_ID.eq(PERMISSION.ID))
.where(ROLE_PERMISSION.ROLE_ID.eq(ROLE.ID))
).as("permissions")
).from(ROLE)
.where(ROLE.ID.eq(id))
.fetchOneInto(Role.class)
Error:
jOOQ; bad SQL grammar [set #t = ##group_concat_max_len; set ##group_concat_max_len = 4294967295; select `users`.`role`.*, (select coalesce(json_merge_preserve('[]', concat('[', group_concat(json_array(`v0`, `v1`) separator ','), ']')), json_array()) from (select `users`.`permission`.`id` as `v0`, `users`.`permission`.`name` as `v1` from `users`.`role_permission` join `users`.`permission` on `users`.`role_permission`.`permission_id` = `users`.`permission`.`id` where `users`.`role_permission`.`role_id` = `users`.`role`.`id`) as `t`) as `permissions` from `users`.`role` where `users`.`role`.`id` = ?; set ##group_concat_max_len = #t;]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'users.role.id' in 'where clause'.
Database: MYSQL, Database Name: 'users', JOOQ Version: 3.16.6
Correlating derived tables isn't supported in MySQL 5.7. Support has been added only in MySQL 8.0.14:
https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html
jOOQ currently can't work around this limitation, see:
https://github.com/jOOQ/jOOQ/issues/12045
The solution is either:
Upgrade your MySQL version
Use a MULTISET_AGG based approach instead

Hibernate generating wrong sql syntax

Hibernate generating wrong sql(MySQL) query with syntax error.
given this HQL query :
"update GpClientContacter set id = :newKey where id = :Key"
Hibernate executes this sql query:
Hibernate: update GP_CLIENT_CONTACTER set CODE_CLIENT, NOM_RS=(?, ?) where (CODE_CLIENT, NOM_RS)=(?, ?)
Here is exception message:
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' NOM_RS=(15, 'test1') where (CODE_CLIENT, NOM_RS)=(15, 'test5')' at line 1
.
.
org.hibernate.exception.SQLGrammarException: could not execute statement
id is an EmbeddedId that has the codeClient and nomRs fields. :key and :newKey are instances of the EmbeddedId Type

jpql equivalent for this sql query (query from national characters)

I have a sql query which works fine on sql server query window and returns results
sql query:
select * from table1 where title = N'تست'
if I put that 'N' in my query in sql, the query gets results and if I don't put 'N' no results will be returned.
the question is how can I have this query in jpql format (with that 'N')?
thanks for replying
Try setting this Hibernate property:
hibernate.connection.defaultNChar=true
If you use an external DataSource (external connection pool like HikariCP) then you want to set the following property in the JDBC connection URL:
sendStringParametersAsUnicode=true

Parameterized sql in clause with variable number of arguments in DB2

I have a sql as below in my java program:
String sql = "Select * from mySchema.myTable where product in (?) and myDate = ?";
I have my query params as:
Object[] params = {"\'abc\',\'pqr\',\'lmn\'",'2013-07-18'};
And I am trying to execute as:
List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, params);
where jdbcTemplate is a org.springframework.jdbc.core.JdbcTemplate object.
However, I am getting error as:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [Select * from mySchema.myTable where product in (?) and myDate = ?]; DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81; nested exception is com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:796)
Further down the stack trace:
Caused by: com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.59.81
at com.ibm.db2.jcc.am.dd.a(dd.java:668)
at com.ibm.db2.jcc.am.dd.a(dd.java:60)
at com.ibm.db2.jcc.am.dd.a(dd.java:127)
at com.ibm.db2.jcc.am.bn.c(bn.java:2546)
at com.ibm.db2.jcc.am.bn.a(bn.java:2053)
at com.ibm.db2.jcc.t4.cb.n(cb.java:802)
at com.ibm.db2.jcc.t4.cb.i(cb.java:259)
at com.ibm.db2.jcc.t4.cb.c(cb.java:54)
at com.ibm.db2.jcc.t4.q.c(q.java:44)
at com.ibm.db2.jcc.t4.rb.j(rb.java:147)
at com.ibm.db2.jcc.am.bn.ib(bn.java:2048)
at com.ibm.db2.jcc.am.cn.b(cn.java:3845)
at com.ibm.db2.jcc.am.cn.b(cn.java:3975)
at com.ibm.db2.jcc.am.cn.bc(cn.java:678)
at com.ibm.db2.jcc.am.cn.executeQuery(cn.java:652)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:643)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
How can I pass a string as a parameter to my sql where my string is of type 'abc','pqr','xyz'
Thanks for reading!
I suggest you use a better ORM like MyBatis.
Also it looks like you're passing a string parameter as a date.
Your solution to pass a list of strings to IN statement won't work either, your
"'abc','cde'" will be treated as a single string in the IN statement since all SQL characters like , are ignored in parameters of parametrized statements (it's the feature that prevents SQL injection).

jpa select statement with subnets

I am trying to write query with multiple select subnets in it.But I defined a nativequery
I am giving error. Compiler specifies that "(" after "from" is not proper. How can I define
a native query in JPA 2.0
For eaxmple:
SELECT *
from (SELECT ****C) REI3 where column1 != 1
GROUP BY REI3.column2 order by REI3.column3 ASC
JPA does not have too much to do with validating SQL syntax, query is passed to JDBC driver. Likely you are trying run query such a way, that it is interpreted as JP QL. Instead try following method to execute it as
Query q = em.createNativeQuery("Your SQL here");
Other alternative is to use NamedNativeQuery Example

Categories

Resources