Spring boot and jpa query #P0 - java

I'm trying to run the following query
#Query(value = "INSERT INTO controllordinitest.dbo.Records(FSId, FSBlob) SELECT NEWID(), BulkColumn FROM OPENROWSET(BULK :path, SINGLE_BLOB) as f;", nativeQuery= true)
#Modifying
void saveFile(#Param(value="path") String path);
but I keep getting the syntax error #P0, I also tried not to use parameters but "?" and still not working, my guess is that the string is not placed under '' and it ends up just next to bulk, but as soon as I place the single quote I get the error that there is no file in :path.
I also tried to wrap the ? with parenthesis but no luck... even tried to change the hibernate dialog but no luck again...
It could be a possible duplicate of this but their solutions just don't work

I think the cause of this problem is that JPA parameters are only allowed inside the WHERE clause of the query.
You can't use the parameter at any place of the created query.
In your case you use them in FROM part FROM OPENROWSET(BULK :path, SINGLE_BLOB)

Related

Add where clause to setParameter JPA

At the moment I only know that you can do this in JPA
SELECT art_bezeichnung FROM table WHERE art_nr = :parameter
.setParameter("parameter", "H03")
But can you also add a whole where clause via a parameter
SELECT art_bezeichnung FROM table :parameter
.setParameter("parameter", "WHERE art_nr = H03")
I searched the Internet for it, but couldnĀ“t find a clear answer.
That's not possible because of possible SQL-Injection.
Use a if statement instead:
if (Parameter is set) add where to SQL Statement and Parameter.

Building CosmosDB Query strings with escaping

I want to build a cosmosdb sql query, because I'm using a rest interface, which accepts SQL querys (don't ask why, I can't change that :( )...
Now I want to build that query with some parameters, which affects the WHERE clause.
I think it is a good idea to escape these parameters, to prevent sql injection.
But I just found these way to build a query:
var param = new SqlParameter();
param.add("#test", "here some string to inject");
var query = new SqlQuerySpec("SELECT #test FROM table", param);
Now I could do sql calls to the cosmos's without sql injection. But I don't want this. I just want to get the query string.
But I need the full query from "query". But there seems to be just the method query.getQueryText(). But this just returns the string "SELECT #test FROM table".
Do know a workaround for me? Or maybe just a good package I can use to to my own string escapes.
T
I found the information that this escalation stuff doesn't happen on client site. It happens in the dbms. So I need a rest interface, where I can pass the parameters.
Azure Cosmos DB SQL Like, prepared statements

Spring JPA group by query pageable injects unnecessary ID causes syntax error

I am trying to use Spring Data Pageable with JPA Query (Not native) which cases the following exception:
Caused by: java.sql.SQLSyntaxErrorException: Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'alerting.alerthisto0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Here is my query:
#Query(value =
"SELECT history.alertId AS alertId, history.userId AS userId, alert.organization.id AS organizationId,"
+ " alert.status AS status, alert.createdAt AS createdAt, COUNT(history.alertId) AS alertsCount"
+ " FROM AlertHistory AS history LEFT JOIN Alert AS alert ON history.alertId=alert.id"
+ " GROUP BY history.alertId, history.userId")
Page<AlertAggregationDetails> groupByAlertIdAndUserId(Pageable pageable);
Apparently, because I am using Pageable history.id is injected which is unnecessary and causing syntax issue. If I add history.id to the group by section the syntax will be correct but obviously, the result is far from what I'd like to have. I was wondering if there is a way to fix the syntax issue and continue using Pageable.
Also, if I remove Pageable this query works and there is no issue.
EDIT:
After I debug the query I noticed that Spring JPA added ORDER BY history.id to my query automatically when I use Pageable. As a workaround, I can replace sql_mode, but I don't understand why I cannot overwrite ORDER BY with Pageable.
AFAIK Spring Data by default adds the entity id as order by expression if you don't add any sorts which might be what you see. It makes sense, because you want a consistent ordering, otherwise pagination wouldn't work. You will have to add the sorts alertId and userId for MySQL to be happy and also get consistent results.

Spring-data-couchbase - running update query

Is any possibility to execute N1QL update query using spring-data?
I.e. I have following update query:
"UPDATE USERS USE KEYS $id SET location = $location"
I tried to use couchbaseTemplate.queryN1QL method, but it doesn't work.
Is there any solution of this problem using spring data or even native couchbase java SDK?
the CouchbaseTemplate is more tailored towards Spring Data document's use case, so it expects to run queries that return specific elements and will end up unmarshalled into entities (eg. a User object).
if you want to run a more freeform query, like your update, you can always access the native SDK from the template. In your case:
String paStatement = "UPDATE USERS USE KEYS $id SET location = $location";
JsonObject paramValues = JsonObject.create().put("id", theId).put("location", "theLocation");
N1qlQuery query = N1qlQuery.parametrized(paStatement, paramValues);
template.getCouchbaseBucket().query(query);
I think you can add RETURNING * at the end of your UPDATE statement. It would make the statement returns something. It works for me.

Exception in inserting data into data using JPA in netbeans

SEVERE: Local Exception Stack:
Exception [EclipseLink-7092] (Eclipse Persistence Services - 2.0.0.v20091127-r5931):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot add a query whose types conflict with an existing query.
Query To Be Added: [ReadAllQuery(name="Voter.findAll" referenceClass=Voter
jpql="SELECT v FROM Voter v")] is named: [Voter.findAll] with arguments [[]].
The existing conflicting query: [ReadAllQuery(name="Voter.findAll" referenceClass=
Voter jpql="SELECT v FROM Voter v")] is named: [Voter.findAll] with arguments: [[]].
I too have come across this issue and it makes little sense. I only have one entity bean with one defined query, and it continues to tell me it's the problem. I did a stop, then start of GF3, redploy my app, and I still get it.. and worse, I am not even using the query.
One thing I don't understand.. why is EclipseLink being used in GF? Is that part of GF? I use Eclipse IDE, but I don't deploy from within Eclipse.. I deploy from my ant build script at command line. I am guessing GF must be using some EclipseLink (used to be TopLink?).
One answer above said to make sure there are no stale files, undeploy app, etc. Would be great if someone that has figured this out could provide more details and explain it. If it is another query that has an error in it, sure would be nice if the error was shown instead of this misleading one.
So far, I've stopped GF, dropped all the tables, restarted, redeployed (in autodeploy folder), and still get this issue right away. I generally build/deploy to autodeploy folder several times in short periods of time, as I make quick changes then build/redeploy.
I encountered this problem also, I found out the exception isn't related with the error file at all, the problem is from another query for example:
#NamedQuery(name = "ChannelType.ALL", query = "SELECT channelType FROM ChannelType channelType WHERE channelType.applicationClient.applicationClientId =:applicationClientId ORDER BY channelType.channelTypId ASC")
the problem is from "ORDER BY channelType.channelTypId" its not except to order by primary key ,when I remove this line the exception just gone also.
Maybe someone else could explain why this happen.
Thanks
Just for the people out there that are still struggling with this error:
Undeploy your application and check if there are any stale (maybe locked) files left. This would cause the old namedqueries to still exist and thus not replacing them.
Delete the files and redeploy. The error should disappear.
Edit:
Also check if you haven't done anything like ... WHERE o.object_id = :id ... instead of ... WHERE o.object = :object ...
This was the solution for my problems. Took me 3 weeks to figure that out. EclipseLink isn't very clear when it comes to exceptions. There was actually a query compile error. Instead it throws a duplicate query exception.
It looks like you have the query defined twice. Either on the same entity, or on another entity, or in orm.xml
Not sure of what you're doing exactly since you're not showing any code but this is what EclipseLink documentation says about error ECLIPSELINK-07092:
ECLIPSELINK-07092: Cannot add a query whose types conflict with an
existing query. Query To Be Added:
[{0}] is named: [{1}] with arguments
[{2}].The existing conflicting query:
[{3}] is named: [{4}] with arguments:
[{5}].
Cause: EclipseLink has detected a conflict between a custom
query with the same name and arguments
to a session.
Action: Ensure that no query is added to the session more than once
or change the query name so that the
query can be distinguished from
others.
According to the above description and to the trace, it seems that you're adding a query (actually the same) with the same query name more than once to the session. You shouldn't (or use another query name).
also the error can come from a namedquery malformed, i had an where y o.activo -> that show me the specified error.
I had the problem...
The real Exception was a #Named Query malformed but the stacktrace just said:
"Exception Description: Cannot add a query whose types conflict with
an existing query"
My solution:
In the persistence unit change Table Generation Strategy to "None" and Validation Strategy to "None". When run again I obtained the real Exception (Malformed Query). I resolved the error in the query, returned to the old configuration in the persistence unit and all exceptions disappeared.
I'm going crazy but at least, this not works:
#NamedQuery(name = "xyx", query = "SELECT count(v) FROM Classe v WHERE v.id =:_id);
this works:
#NamedQuery(name = "xyx", query = "SELECT count(v) FROM Classe v WHERE v.id = :_id);
"WHERE v.id =:_id" was the error

Categories

Resources