I'm using Mysql database. I got stored data for certain columns with value of a\"a. I used following query to select but it failed:
select * from table_name where coloum_name like "%a\"%";
I spend some hours to find a query to select and the following one is working:
select * from table_name where coloum_name like "%a\\\\\\\\""%";
I'm using hibernate in my application, so I used:
criteria.add(Restrictions.ilike("coloum_name","%a\\\\\\\\""%"));
but it's not working. Any possible way to select via criteria??
Check this one. Hope this will help for you.
http://levanhuy.wordpress.com/2009/02/19/providing-an-escape-sequence-for-criteria-queries/
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 have a select SQL in MyBatis like this:
SELECT * FROM MYTABLE WHERE id IN (#{ids})
The parameter ids I passed in is correct, which I can get while I'm debugging. And also, I can run this SQL in MySQL with the parameter correctly and get the results.
But nothing is gotten running with Java code.
I printed the SQL of MyBatis and the SQL is like this:
SELECT * FROM MYTABLE WHERE id IN (?)
and the parameters shown in log is correct too('1', '2', '3').
Why can't I get the data by code?
I'd be appreciated if anyone can help.
You need to use <foreach> as described in the documentation.
See this other SO question if you are using annotations.
We are dealing with sybase database in core java code. We are using org.eobjects.metamodel.DataContext to parse query.
String sCol[]=table.getColumnNames();
Query query=dataContext.query().from(table.getName()).select(sCol).toQuery();
return new QueryIterator(dataContext.executeQuery(query).iterator());
But it executing query. Same code working fine with Oracle database to parse and execute query.
Some of query example generated are :
select City.CityName from ownername.City
select City.CityName from City
select CityName from ownername.City
select CityName from City
select ownername.City.CityName from ownername.City
SELECT "City"."CityName" FROM ownername."City"
select * from ownername.City
No any of above query executed. We are getting these errors :
Not capable of parsing FROM token: "ownername"."City"
Could not execute query: The column prefix '"City"' does not match with a table name or alias name used in the query. Either the table is not specified in the FROM clause or it has a correlation name which must be used instead.
Not capable of parsing SELECT token: ownername.City.CityName
How can we execute query using metamodel wih SYBASE database OR is there any other way to execute sybase queries?
Oracle (and Microsoft) use a schema logical construct that Sybase ASE does not. In SAP/Sybase ASE, all tables and columns in a database are in the same schema. It is possible to users to have their own objects in the database though, so there is the possibility of imitating some of the schema behavior using user ownership, but it would require an extra level of effort.
For Sybase the proper query syntax would be:
SELECT [ Col1, Col2 | * ]
FROM [dbName].[ownerName.]TABLE
In your case
SELECT CityName
FROM dbName.ownername.City
In Sybase ASE, it's typically best practice to have all objects owned by 'dbo', so in that case you can omit the owner from the query:
SELECT CityName
FROM dbName..City
Full query syntax and information can be found in the Query Section of the Transact SQL Users Guide in the documentation.
The error messages you're getting are coming from MetaModel's query parser layer. It is searching for matching column and table names in the metadata of your database before it's even firing the query.
I notice that you're using namespace "org.eobjects.metamodel". You should upgrade to Apache MetaModel ("org.apache.metamodel") if possible since a lot has been improved in MetaModel since it's introduction into Apache. Including a lot of query parser improvements.
In Oracle, if you name a table User, you must query the table by putting quotes around the word user.
This will not work
select * from User
This will work
select * from "User"
My question is, how do I run a hibernate HQL query on a table named User? I have tried putting "" around User, escaping quotes, single quotes, nothing works. HQL doesn't like those characters and errors out. I have googled and searched for a solution and found nothing.
You need to escape the table name, in your entity mapping:
#Entity
#Table(name="`User`")
public class User {
...
}
The when you write an HQL query like this:
from User
Hibernate will generate an SQL query like this:
select * from "User"
I am executing select queries in my java app using org.hibernate.SQLQuery. The code is as below.
Query query=session.createSQLQuery("query");
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();
The problem is, the oracle AWR report shows that there is another query that fires up with every select query I execute.
SELECT NULL AS table_cat, o.owner AS table_schem, o.object_name AS table_name, o.object_type AS
table_type, NULL AS remarks FROM all_objects o WHERE o.owner LIKE :1 ESCAPE '/' AND o.object_name
LIKE :2 ESCAPE '/' AND o.object_type IN ('xxx', 'TABLE') ORDER BY table_type, table_schem, table_name
Can anyone explain why this happens and how can I avoid this(I want to avoid this as this is the most CPU consuming query listed in the report)?
I could find this which exactly deals with my problem. I am going test the suggestions given in the link. I will update the result here once I figure out the right solution.
How to avoid this very heavy query that slows down the application?