The function CreateQuery(string) in java.persistence.Query is returning a different query from what I requested.
My input string to the function is:
from Underlying where (SUBSTRING(ticker, (charindex('.', ticker) +
1), (len(ticker) - charindex('.', ticker))) in (:exchanges) order by
ticker
When I call getResultList() on the Query, I see the following sql in my log:
Hibernate: select underlying0_.id as id63_, underlying0_.updated_date
as updated2_63_, underlying0_.updated_user as updated3_63_,
underlying0_.version as version63_, underlying0_.adr as adr63_,
underlying0_.desk as desk63_, underlying0_.enabled as enabled63_,
underlying0_.forward_start_enabled as forward8_63_,
underlying0_.ticker as ticker63_, underlying0_.vol_shift_type as
vol10_63_ from underlyings underlying0_ where
(substring(underlying0_.ticker, charindex('.', underlying0_.ticker)+1)
in (?)) order by underlying0_.ticker
This is query is not a valid SQL Server 2008 query and as expected, I received an error:
SQL Error: 174, SQLState: S0001
The substring function requires 3 argument(s).
Any one know what is going on here?
FYI, I have tried the following two dialects 1) org.hibernate.dialect.SQLServer2008Dialect, and 2) org.hibernate.dialect.SQLServer2008Dialect
Issue is with function charindex. It is not available in the dialect, therefore causes incorrect mapping. Changing this to the HQL function "locate" (keeping args constant) fixed the issue. My query now works in SQL Server 2008:
...where (substring(underlying0_.ticker, charindex('.',
underlying0_.ticker)+1, len(underlying0_.ticker)-charindex('.',
underlying0_.ticker)) in (?))...
Error clearly mention that SUBSTRING() function require 3 Arguments you are passing 2. thus error is in your query.
read about SUBSTRING() Function
Related
My Query looks like below,
#Query(value="SELECT * FROM Retention_Record retention where retention.entity=:entity and retention.category=:category and retention.record_type=:recordType and json_exists(retention.reference_data_id, '$?(#==$referenceDataId)' passing :referenceDataId as \"referenceDataId\")" , nativeQuery=true)
When i am getting below error while execution of this query
2020-07-23 16:12:11 [eventsTaskExecutor-5] [] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 29902, SQLState: 99
999
2020-07-23 16:12:11 [eventsTaskExecutor-5] [] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-29902: error in executing
ODCIIndexStart() routine
ORA-20000: Oracle Text error:
DRG-50900: text query parser error on line 1, column 10
DRG-50917: escape on at end of text query string
Can anyone please help.
I fixed the issue. Problem was with the way i was storing the Json Object in database. I was storing the value in db as ["50000021","50000022"] which in fact is a valid json, and was trying to execute my query to provide result, which was resulting in error. When i stored data in this format, { "ReferenceDataId" : ["50000021","50000022"] } below query successfully executed. – Ritesh 1 min ago Edit Delete
#Query(value = "SELECT * FROM Retention_Record retention where retention.entity=:entity and retention.category=:category and retention.record_type=:recordType and json_exists(retention.reference_data_id, '$.ReferenceDataId?(#==$referenceDataId)' passing :referenceDataId as \"referenceDataId\")", nativeQuery = true)
I am dealing with a legacy code that is 'not changeable' (no way to move to criteria api) and I have a little trouble with proper parameters binding. The query looks like this (MS SQL):
SELECT BRAND AS b FROM CAR WHERE (NAME LIKE :phrase OR MODEL LIKE :phrase ) AND AGE NOT IN(1997, 1998) AND (:mileage IS NULL OR MILEAGE LIKE :mileage) ORDER BY BRAND
(...)
query.setParameter("phrase", "%" + phrase + "%");
query.setParameter("mileage", mileage);
phrase is actually required, but because the mileageparameter is optional, it's done in wierd way presented above.
The problem is that with both phrase and mileage provided, It keeps giving me following error : java.sql.SQLException: ResultSet may only be accessed in a forward direction.. It works wihtout mileage parameter provided. Why I am getting this error ?
EDITED:
Running this query on db gives me no results.
I am using query.setResultTransformer(Transformers.aliasToBean(type)) as well as setting first and max result on my SQLQuery query object.
An error is when calling query.list() (used intellij evaluate expression)
shouldn't query.list() return an empty result ?
Probable answer (in that case):
It looks like setting FirstResult on the query cause that problem because - by mistake - it's a negative number.
How are you executing the SQL and then accessing the results?
It sounds like there are no results when executing the SQL with the mileage parameter and you are somehow trying to read the results anyway.
Try running the generated SQL immediately on the database and see if it returns any rows.
My problem seems to be very simple but I have hard time resolving it.
I want to alter an PostgreSQL sequence using Hibernate via native sql query (other solutions are also welcomed) using next code:
Query query = getSession()
.createSQLQuery("ALTER SEQUENCE users_id_seq RESTART WITH ?")
.setInteger(0, 1);
query.executeUpdate();
But I am getting this error:
Hibernate:
ALTER SEQUENCE users_id_seq RESTART WITH ?
hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42601
hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: syntax error at or near "$1"
Best I'm aware, you cannot prepare that statement.
If hibernate allows it, emulate the prepared statement, instead of sending it to the server. If not, sanitize the variable and issue the final statement directly.
Alternatively, wrap it in a function with dynamic SQL:
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
I am connecting to a DB2 database (DB2 v9.7.400.501) from my Java web application using the IBM DB2 Type 4 driver (db2jcc4.jar). When I try to execute an SQL statement like this,
SELECT * FROM USERS WHERE UPPER(USERNAME) = UPPER('testuser');
I get the following exception:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error:
SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;=
UPPER('testuser');END-OF-STATEMENT, DRIVER=4.12.55
The problem is from the UPPER function since, a normal select statement executes normally.
Maybe you should use is this way:
SELECT * FROM USERS WHERE UPPER(USERNAME) LIKE UPPER('testuser');
Your code with '=' is seems ok for SQLite but don't know anbout db2.
UPD. After some investigation, I can say that error is cause by Java code which tries to execute multiple statements in one query using ';' as a delimiter.
You should try using the PreparedStatement, addBatch() and executeBatch() for multiple statements.
UPD2. This is DB2 related issue. PostgreSQL, afaik, allows multiple statements in single query.
This query works when I input it through phpmyadmin.
INSERT INTO conversation (user_id) VALUES (?);
INSERT INTO conversation (conversation_id, user_id)
VALUES ((SELECT LAST_INSERT_ID()), ?)
However when I send that query using jdbc and java I get an 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 'INSERT INTO conversation (conversation_id, user_id) VALUES ((SELECT LAST_INSERT_' at line 1"
I am using the exact same query. I checked by calling toString on the PreparedStatement and copying and pasting it into phpmyadmin and executing it and it worked fine. It just doesn't work through java. Any ideas whats wrong?
By default, you cannot execute multiple statements in one query through JDBC. Splitting it into two calls will work, as will changing the allowMultiQueries configuration property to True.
JDBC Configuration Properties — allowMultiQueries:
Allow the use of ';' to delimit multiple queries during one statement (true/false), defaults to 'false', and does not affect the addBatch() and executeBatch() methods, which instead rely on rewriteBatchStatements.
Default value: false