connection.getMetaData() getting called for every select query in Hibernate - java

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?

Related

Hibernate query params - bracket notation #{# }

let's say I have the following hibernate query:
#Query("""
SELECT s FROM Submission s
WHERE s.id = :#{#submissionId}
""")
Submission getSubmissionById(#Param("submissionId") Long submissionId);
In our code I find two different ways of putting the parameter into the query:
:#{#submissionId} and :submissionId (without the brackets and #)
Is there a particular reason to use either of those? As far as I know they do exactly the same thing.

Hibernate native sql query exception - rs cursor forward only

I am dealing with a legacy code that is 'not changeable' (no way to move to criteria api) and I have a little trouble with proper parameters binding. The query looks like this (MS SQL):
SELECT BRAND AS b FROM CAR WHERE (NAME LIKE :phrase OR MODEL LIKE :phrase ) AND AGE NOT IN(1997, 1998) AND (:mileage IS NULL OR MILEAGE LIKE :mileage) ORDER BY BRAND
(...)
query.setParameter("phrase", "%" + phrase + "%");
query.setParameter("mileage", mileage);
phrase is actually required, but because the mileageparameter is optional, it's done in wierd way presented above.
The problem is that with both phrase and mileage provided, It keeps giving me following error : java.sql.SQLException: ResultSet may only be accessed in a forward direction.. It works wihtout mileage parameter provided. Why I am getting this error ?
EDITED:
Running this query on db gives me no results.
I am using query.setResultTransformer(Transformers.aliasToBean(type)) as well as setting first and max result on my SQLQuery query object.
An error is when calling query.list() (used intellij evaluate expression)
shouldn't query.list() return an empty result ?
Probable answer (in that case):
It looks like setting FirstResult on the query cause that problem because - by mistake - it's a negative number.
How are you executing the SQL and then accessing the results?
It sounds like there are no results when executing the SQL with the mileage parameter and you are somehow trying to read the results anyway.
Try running the generated SQL immediately on the database and see if it returns any rows.

Spring data JPA - The encapsulated expression is not a valid expressio

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')

Building ordered and limited delete request in jooq

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)
))

Hibernate like query for escape character

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/

Categories

Resources