SQL to Hibernate Query Language (HQL) - missing IN - java

Desired SQL statement to convert
select * from tableA where columnA is not null
order by cast(columnA as int), columnB
union all
select * from app_data_program where columnA is null order by columnB
My HQL attempt:
From TableA a where a.columnA is not null
order by cast(a.columnA as int), a.columnB
union all TableA b where b.columnA is not null order by b.columnB
When I converted the HQL to SQL to test as a result, i get the following:
SQL Error: Missing IN or OUT parameter at index:: 1

HQL doesn't allow use UNION ALL sql construct.
You must execute two different queries and then you can merge the results of them.
So you'll have:
First query:
From TableA a where a.columnA is not null
order by cast(a.columnA as int), a.columnB
Second query:
TableA b where b.columnA is not null order by b.columnB

Related

Pass Integer Array for Number column in database

I have query looks like SELECT * from TableName WHERE id= ?
for id field I want to pass array of ids which Number field in Database,
I have created Number Array type i database, when I pass Array to my prepared statement I get following error.
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * from Tablename WHERE id= ?]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected NUMBER got schema.app_id
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:95)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springfra
You need to use a table collection expression in your query:
The table_collection_expression lets you inform Oracle that the value of collection_expression should be treated as a table for purposes of query and DML operations. The collection_expression can be a subquery, a column, a function, or a collection constructor. Regardless of its form, it must return a collection value—that is, a value whose type is nested table or varray. This process of extracting the elements of a collection is called collection unnesting.
So you need to convert your passed collection (array) to a table in a subquery within your in() clause:
SELECT * from TableName WHERE in (SELECT column_value FROM TABLE(?))
Or with a join:
SELECT t.*
FROM TABLE(?) i
JOIN TableName t
ON t.id = i.column_value

Exception using a prepared statement with #BindIn annotation

I'm using org.skife.jdbi.v2.unstable.BindIn in my prepared statements. It works as expected unless I try to concatenate the parameter in the query.
Below are 3 queries. The first 2 queries work as expected, but the third query gives this error:
A SELECT statement that assigns a value to a variable must not be
combined with data-retrieval operations
SELECT
name
FROM
myTable WITH(NOLOCK)
WHERE
ID in ( <ids> )
declare #query VARCHAR(MAX)
select #query = '
SELECT
name
FROM
myTable WITH(NOLOCK)
WHERE
ID in (1, 2, 3)'
execute(#query)
declare #query VARCHAR(MAX)
select #query = '
SELECT
name
FROM
myTable WITH(NOLOCK)
WHERE
ID in (' + <ids> + ')'
execute(#query)

ResultSetMetaData.getColumnName returns different values for UNION and non-UNION queries

Using Java JDBC, I want to collect information returned from SQL Select query.
If I fire below SQL query:
SELECT col1 AS 'Field1', col2 AS 'Field2' FROM Table;
Then, using resultSetMetaData.getColumnName(1), I get 'col1' as result, which is the expected result.
Now, the problem is, when I join 2 SQL tables (Since, MySQL does not provide Full Outer Join, hence, I fired the following query)
SELECT Table1.Col1 AS 'Field1', Table1.Col3 AS 'Field2',
Table2.Col5 AS 'Field3',Table2.Col4 AS 'Field4' FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION
SELECT Table1.Col1 AS 'Field1', Table1.Col3 AS 'Field2',
Table2.Col5 AS 'Field3',Table2.Col4 AS 'Field4' FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id;
Now, using resultSetMetaData.getColumnName(1), I get 'Field1' as result, where as, I expected 'col1'.
I tried resultSetMetaData.getColumnLabel(1) also, but it still returned 'Field1'.
I want 'col1' as the result, which I could not get by any of the methods of resultSetMetaData.
Any help on this will be appreciable.
You are seeing those results because it is a UNION query. It is entirely possible that such a query could do something like
SELECT Col1 AS Field1 FROM Table1
UNION
SELECT Col2 AS Field1 FROM Table2
In that case there is no single "correct" answer if getColumnName was to try and return the name of the underlying column in the result: Should it return 'Col1' or 'Col2'?
Since any column in the result set of a UNION query can be derived from more than one underlying column, getColumnName can only return the effective name of that column, which is Field1 in the example above.

SQL subquery in Hibernate Query Language

I am new to HQL and I am working on subqueries.
I have the following SQL subquery:
select * from (
select * from table order by columnname
) as subquery
where columnvalue = 'somevalue';
I want to fire the query in HQL. I wrote the below code :
Result = session.createQuery("from (from table order by columnname) as subquery where columnvalue = :somevalue")
.setParameter(/*setting all parameters*/)
.list();
I am getting this exception:
QuerySyntaxException : unexpedted token :( line 1, column 10 [from (from ...)]
My SQL query is giving me correct results. How do I write it in HQL ?
I did not think HQL could do subqueries in the from clause
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries
note the sentence:
Note that HQL subqueries can occur only in the select or where clauses.
It will be better if you excute native SQL.

PreparedStatement with subquery returning empty result set

I've got a query that works fine when I run it in SQL Developer, but returns an empty result set when run as a prepared statement. I'm not sure if my query is formatted incorrectly, or if it's something else (which I'll leave for another question entirely).
So here is my query. I've stripped stuff out in order to capture the format of it, and not the business logic. The table has three columns: type, key, and value.
SELECT a.key id, a.value name
FROM
(SELECT * FROM sometable WHERE type='A') a,
(SELECT * FROM sometable WHERE type='B') b,
(SELECT * FROM sometable WHERE type='C') c,
(SELECT * FROM sometable WHERE type='D') d
WHERE a.value = b.key
AND a.value = c.key
AND a.value = d.key
Essentially, should this execute correctly in a prepared statement?
Are you seeing any errors?
The query as is can be run as a Statement since it is a static SQL.

Categories