Issue while implementing json_exists using JPA Native query - java

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)

Related

JPA how to filter in a Json field (Postgres)

I'm trying to perform a native query in a Postgres (v15) database, where the products table has a category field of type json, which stores a list of categories, but jpa complains that the character " ?" is an unspecified parameter.
select * from products where category::jsonb ? :category limit 5
Returning me the following message:
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 22023
o.h.engine.jdbc.spi.SqlExceptionHelper : No value specified for parameter 2.
I tried to escape using "\" but I was unsuccessful using this:
select * from products where category\\:\\:jsonb\\? :category limit 5
Has anyone else experienced this, and had a solution?

How to add date in mysql database from Hibernate/Spring Jpa

I use spring boot, and I want to add 1 year to a specific column in mysql database
String queryRecherche = "UPDATE myTable t SET t.dateDebut = DATE_ADD(t.dateDebut, INTERVAL 1 YEAR) WHERE.id = 3 ";
Query query = em.createQuery(queryRecherche);;
query.executeUpdate();
But I get the folowing error :
org.hibernate.query.sqm.ParsingException: line 1:66 no viable alternative at input 'DATE_ADD(t.dateDebut,INTERVAL1'
Have you please any suggestions to do this.
You're using Hibernate 6 (I can tell by the error message), so the correct HQL syntax to use is:
UPDATE MyEntity t SET t.dateDebut = t.dateDebut + 1 year WHERE t.id = 3
You had three errors in your query:
You referred to the name of a table instead of the name of an entity class in the UPDATE clause.
You used the unportable MySQL DATE_ADD function instead of the portable HQL date/time arithmetic described here.
The syntax of your WHERE clause was garbled.
Perhaps you meant for this to be a native SQL query, in which case you called the wrong method of Session. But there's no need to use native SQL for the above query. As you can see, HQL is perfectly capable of expressing that query.
You can use SQL directly, via createNativeQuery, or register a new function as shown in this example to call it from HQL

Query generated by Hibernate is incorrect for JSON type

I am using a Named Query to call a Function in Postgresql. The function takes JSON as an Input but when I pass the query to JPA it strips of 1 of the colons resulting in an incorrect query that is generated.
Here is my code
query = entityManager.createNativeQuery("select * from transfer_validate(\'{\"a\":\"100\",\"b\":\"200\"}\'::json,\'{\"a\":\"100\",\"b\":\"200\"}\'::json);");
query.getResultList();
Below is how the Function is created:
CREATE OR REPLACE FUNCTION transfer_validate(_d json, _l json)
This is the error that is generated :
org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
Position: 67
Below is the SQL that is generated for Postgres in Hibernate logs
select * from transfer_validate('{"a":"100","b":"200"}':json,'{"a":"100","b":"200"}':json);
You can clearly notice that ::json is replaced by :json and hence the error.
How can I correct this ?
thanks.
It's not an ideal solution, but you can use this:
list = entityManager.createNativeQuery("select * from transfer_validate(?\\:\\:json,?\\:\\:json);")
.setParameter(1, jsonString1)
.setParameter(2, jsonString2)
.getResultList();
Basically you're telling Hibernate that the colons are not to be interpreted as parameter names. Alternatively you can use the standard SQL cast CAST(:a as json)

How to make hql selects in spring-batch?

I want to use spring-batch for retrieving and processing data from a postgres db.
I have a working SQL statement that would give me the full result set (about 400k entries):
private static final String QUERY = "SELECT * FROM MyDataTable ";
Now I want to use the JpaPagingItemReader so that the data is fetched (and written elsewhere) in chunks:
JpaPagingItemReader<MyEntity> reader = new JpaPagingItemReader<>();
reader.setEntityManagerFactory(emf);
reader.setQueryString(QUERY);
But it does not work:
[] 2014-09-17 16:31:58,234 ERROR : QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM my_data_table]
I also tried SELECT FROM MyDataTable and SELECT m FROM MyDataTable m without the star. Same result.
So, how can I execute that hql query with spring-batch?
By the way: the query works fine in a sql editor like pgAdmin.
SELECT m FROM MyDataTable m is almost correct (it is valid JPQL query as long as you have entity calles MyDataTable). So, it seems that you don't have entity class named MyDataTable.
As JpaPagingItemReader#setQueryString(String) accepts JPQL queries you should make sure that you have entity class for this table and then you should use its name instead MyDataTable.
By the way - for HQL queries there's HibernatePagingItemReader.

JPA2 CreateQuery SQL Server Dialect

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

Categories

Resources