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.
Related
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
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)
While running the below query to get the latest message with the latest data using the createdOn column
i get the below error
select count(m) from MessageWorkFlowStatus mwfs1 where mwfs1.createdOn =(select max(createdOn) from MessageWorkFlowStatus mwfs2 where mwfs1.status= 'NEW' or mwfs1.status='IN PROGRESS')
The encapsulated expression is not a valid expression
Please let me know if i can run Query this way
First of all, count(m) is not correct. It should either be count(*) or count(mwfs1). Secondly, in your inner query, you are using status column from the outer query table (mswfs1) which is logically wrong. It should instead be mwfs2.status = 'NEW' or mwfs2.status = 'IN PROGRESS'.
I think your query should be:
select count(mwfs1)
from MessageWorkFlowStatus mwfs1
where mwfs1.createdOn = (
select max(createdOn)
from MessageWorkFlowStatus mwfs2
where mwfs2.status= 'NEW' or mwfs2.status='IN PROGRESS')
I recently encountered the following problem with buiding queries in jooq (version 3.1.0):
I want to build delete statement with order and limit constraints. So, my aim is to build something like this:
DELETE FROM table ORDER BY field DESC LIMIT 1 (this is MySql syntax)
But i haven't found nesessary methods in result delete query object:
DSLContext context = createContext();
DeleteWhereStep delete = context.delete(createTable(table));
DeleteConditionStep whereStep = delete.where(condition);
whereStep.orderBy(...)//and no such method here
There are all nesessary methods in select statements and none for delete.
Is it possible to set order and limit for delete request in jooq?
As of jOOQ 3.2, these sorts of extensions are currently not implemented yet. Chances are, that #203 could be implemented in jOOQ 3.3, though.
In the mean time, you have two options:
Resort to plain SQL
i.e. write something like:
context.execute("DELETE FROM {0} ORDER BY {1} DESC LIMIT 1",
createTable(table),
field);
Manually transform your SQL statement into something equivalent
I suspect that the ORDER BY .. LIMIT extension to the MySQL DELETE statement is just sugar for:
DELETE FROM table t
WHERE t.id IN (
SELECT id FROM table
ORDER BY field LIMIT 1
)
Or with jOOQ:
context.delete(TABLE)
.where(TABLE.ID.in(
select(TABLE.ID)
.from(TABLE)
.orderBy(TABLE.FIELD)
.limit(1)
))
I'm trying to do an update in hibernate HQL with a subselect in a set clause like:
update UserObject set code = (select n.code from SomeUserObject n where n.id = 1000)
It isnt working, it is not supported?
Thanks
Udo
I had the same problem, discovered that you need to put bulk updates in side a transaction:
tr = session.getTransaction();
tr.begin();
updateQuery.executeUpdate();
tr.commit;
From the Hibernate documentation:
13.4. DML-style operations
...
The pseudo-syntax for UPDATE and
DELETE statements is: ( UPDATE |
DELETE ) FROM? EntityName (WHERE
where_conditions)?.
Some points to note:
In the from-clause, the FROM keyword is optional
There can only be a single entity named in the from-clause. It can,
however, be aliased. If the entity
name is aliased, then any property
references must be qualified using
that alias. If the entity name is not
aliased, then it is illegal for any
property references to be qualified.
No joins, either implicit or explicit, can be specified in a bulk
HQL query. Sub-queries can be used
in the where-clause, where the
subqueries themselves may contain
joins.
The where-clause is also optional.
While the documentation doesn't explicitly mentions a restriction about the set part, one could interpret that sub-queries are only supported in the where-clause. But...
I found an 4 years old (sigh) issue about bulk update problems (HHH-1658) and according to the reporter, the following works:
UPDATE Cat c SET c.weight = (SELECT SUM(f.amount) FROM Food f WHERE f.owner = c)
I wonder if using an alias in the from-clause would help. Looks like there is some weirdness anyway.