When I try to call a stored procedure from Java - Hibernate. I got the below error.
org.hibernate.QueryException: Not all named parameters have been set:
and my Code
Query query = session.createSQLQuery("select commsrules.evalcommsrules(?::text,?::commsrules.t_commstype)");
query.setString(0, in_query);
query.setString(1, "TRIG");
result = query.list();
and I got the below exception
org.hibernate.QueryException: Not all named parameters have been set: [:text, :commsrules.t_commstype] [select commsrules.evalcommsrules(?::text,?::commsrules.t_commstype)]
at org.hibernate.internal.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:390)
at org.hibernate.internal.SQLQueryImpl.verifyParameters(SQLQueryImpl.java:179)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:118)
please help me to resolve this issue
Looks like it's parsing :text and the other as a named parameter, even though you're trying to use positional parameters.
You could modify the query to use CAST instead of the :: shorthand.
Related
I am using EntityManager in spring boot app to get result from this query
select (c.data::jsonb)->>'employee_Id' as empId from employee e where e.dept ='employee' and (e.data::jsonb)->>'section_id' = '1235'
Its giving me correct output in PgAdmin but in java code
List resultList = em.createNativeQuery(str).setParameter(1, sectionId ).getResultList();
Giving error ERROR: syntax error at or near ":" its breaking at data::jsonb .How do handle this using EntityManager.
You need to CAST like CAST(c.data as jsonb)->>'product_id' from string to JSONB. However i would highly suggest that you don't use TEXT for your JSON type data.
ALTER TABLE employee ALTER COLUMN data TYPE JSON USING data::JSON;
`
I want to make dynamic query in which if particular parameter is sent, the Native query should filter the result based on it. In case it's null, it should not reflect the result.
I am using Spring Data JPA with Native query mechanism + Oracle DB
For String parameters this approach works fine
:email is null or s.email = :email
but for Integer parameters when they have value, the Query works but if the parameter is null the query fails with the error
Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY
I am using the exactly the same approach for for Integer instead of String
I am wondering whether the problem is on my side or it's some kind of bug?
In Oracle DB it's not worked this way. A workaround is using JPQL like
SELECT s FROM Entity s WHERE :id is null OR s.id = COALESCE(:id, -1)
Or for native query use TO_NUMBER function of oracle
SELECT s FROM Entity s WHERE :id is null OR s.id = TO_NUMBER(:id)
I'm not very familiar with Hibernate, but I need to set the random seed on a PostgreSQL database using Hibernate.
This is what I'm trying to run:
String seedSql = "select setseed(0.123)";
Query seedQuery = entityManager.createQuery(seedSql);
seedQuery.getSingleResult();
However, this query does not return any entity mapped by Hibernate, thus I'm receiving the following error:
org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode (...)
Any suggestions?
According to the docs, createQuery() takes a JPQL string, and apparently doesn't know what to do with your Postgres function call.
To run an SQL query, use createNativeQuery() instead.
After some trial and error I noticed that I had to use .createNativeQuery() since setseed() is a function from PostgreSQL:
String seedsql = "select cast(setseed(0.123) as text)";
Query seedQuery = DatabaseInterface.getEm().createNativeQuery(seedsql);
seedQuery.setMaxResults(1).getSingleResult();
I used cast(setseed(0.123) as text) to avoid another error:
org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
I have an Oracle table that has a CLOB in it. Inside this CLOB can be a SQL statement. This can be changed at any time.
I am currently trying to dynamically run these SQL statements and return the column names and data back. This is to be used to dynamically create a table on the web page.
Using Hibernate, I create the query and get the data like so:
List<Object[]> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();
This gets the data I need, but not the column names. I have tried using the getReturnAliases() method, but it throws an error that the "java.lang.UnsupportedOperationException: SQL queries do not currently support returning aliases"
So my question is: Is there a way through Hibernate to get these values dynamically?
You can use :
q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();
to get column names in createSQLQuery.
For more details please refer to this question.
You can use the addScalar method to define the columns.
Look at 16.1.1
https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html
You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.
In 2018 I would suggest using NativeQueryTupleTransformer with native queries.
query.setResultTransformer(new NativeQueryTupleTransformer());
The result format is List<Tuple>. This format is very convenient to work with native SQL queries.
To combine multiple columns as one,
I found one answer
SELECT id,CONCAT_WS(',', field_1, field_2, field_3, field_4) list
FROM `table`;
This query working fine in SQL but it gives me error in HQL:
Error is .
(java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode )
please help me to find out what wrong i did, help me to know how to use CONCAT_WS() IN HQL
below how i written my HQL query
SELECT C1._URI,C1.HEALTH_FACILITY,C1.DISTRICT,CONCAT_WS(',', C1.BLOCKS_OF_BHUBRI, C1.BLOCKS_OF_GOLAGHAT, C1.BLOCKS_OF_HAILAKANDI) as Block_name
FROM GapAnalysisWashInHealthFacilitiesCore C1
any help will appreciate
CONCAT_WS is a function specific to mySql. HQL is a generic language and not aware of native SQL functions and syntax. If you really need the function, then you should use Hibernate's API for native SQL.
Session session = ...;
Query query = session.createSQLQuery("
SELECT id,CONCAT_WS(',', field_1, field_2, field_3, field_4) Block_name FROM `table`");
List result = query.list();
Then you may like to have a look at Result Transformers to get result as list of GapAnalysisWashInHealthFacilitiesCore objects.