I need to get the database size, I am using hibernate and Java. Used this query
("SELECT sum( data_length + index_length ) / 1024 / 1024 as DataBase_Size_in_MB FROM information_schema.TABLES where table_schema="+database)
This works fine in Mysql but hibernate gives information_schema.TABLES is not mapped error.
If you are using hibernate then the name after FROM hibernate tries to find out your POJO class where you would be giving mapping to DB tables.
If you want to use native SQL use use Session.createSQLQuery("");
I don't know exactly what you need but i think you could use DatabaseMetaData or you can use native query which i don't recommend
Related
I have Workspace/Schema EDUCATION in Oracle XE.
In my Java code I want execute queries like this: SELECT * FROM Table instead of SELECT * FROM EDUCATION.Table.
When I write query without EDUCATION I have error: table or view does not exist.
I tried to set the default schema to % (screenshot), but it did not help.
How to avoid writing Workspace/Schema name?
If I understand correctly, you want to access tables in other schemas without using the schema name.
One simple way to do this uses synonyms. In the schema you are connect to:
create synonym table for education.table;
Then you can use table where you would use education.table.
Is it possible to fetch the record using HQL against MySQL with a query like this?
select * from students where binary sid='s001'
I am using hibernate 4.3 and this BINARY operator is not recognized by Hibernate. If I want to achieve the same with Session.get(), what do I have to do?
Both MySQL and HQL support the CAST() function so you can rewrite your query as:
select *
from students
where CAST(sid as binary) = CAST('s001' as binary)
For Session.get() you need to use the #Loader Hibernate custom annotation. You can find more on the #Loader usage here.
I have following Oracle SQL query:
SELECT SUBSTR(col, 0, INSTR(col, REGEXP_SUBSTR(col, '\.\d+$')) -1) AS col_new, col as col_orig AS col_orig FROM tab;
I have data in table like:
col
ABC.A.01
ABC.A.02
Above query returns results like:
col_new col_orig
ABC.A ABC.A.01
ABC.A ABC.A.02
I am trying to migrate it to JPA named query. Till now I could make query only like this:
SELECT SUBSTRING(f.col, 0, LENGTH(f.col) - LOCATE('.', REVERSE(f.col))), f.col FROM tab f;
I did this as I was not able to find equivalent in JPA for Oracle's REGEXP_SUBSTR. My JPA named query fails in data examples like ABC.A.P01.
Can you please let me know how can I migrate my SQL query to JPA named query using equivalent for REGEXP_SUBSTR.
I found that there is no equivalent for REGEXP_SUBSTR in JPA. So I decided to stick to native query execution.
If you are using eclipselink, use SQL to integrate SQL within a JPQL statement. This provides an alternative to using native SQL queries simply because the query may require a function not supported in JPQL.
The SQL function includes both the SQL string (to inline into the JPQL statement) and the arguments to translate into the SQL string. Use a question mark character ( ? ) to define parameters within the SQL that are translated from the SQL function arguments.
You can use SQL to call database functions with non standard syntax, embed SQL literals, and perform any other SQL operations within JPQL. With SQL, you can still use JPQL for the query.
Example
select o from Entity o order by SQL('REGEXP_SUBSTR(?, ''[0-9]+'', 1, 1)', o.code)
I am trying to run query below ,Postgres database.
select new map(avg(cast (speed as double precision)) as avg)
from table
speed column is of type varchar
I am executing using JPA as below
em = entityManagerFactory.createEntityManager();
Query hqlQuery = em.createQuery(query);
reportList = hqlQuery.getResultList();
When I run this I get error below
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE,
found 'precision' near line 1
Has anyone come across this situation? Can I not use precision in Hibernate?
If you're trying to execute native SQL in Hibernate via JPA, you need to use em.createNativeQuery.
em.createQuery expects an argument in JPQL, a query language derived from HQL, to the point where Hibernate's JPQL implementation uses the org.hibernate.hql classes.
That doesn't look like valid SQL either, though. new map(...) ? You can't mix JPQL/HQL, and I haven't seen new in an SQL dialect.
I changed from
select new map(avg(cast (speed as double precision)) as avg)
from table
to
select new map(avg(cast (speed as double)) as avg)
from table
Hibernate supports that. I am still running as HQL query and it works.
I got very typical issue. My dynamically generated query like this...
UPDATE Templates t SET t.TEMPLATE_DATA = replace(t.TEMPLATE_DATA, 'Test\'s Test', 'Kent"s Test'), t.TEMPLATE_DATA = replace(t.TEMPLATE_DATA, 'Test"s Test', 'Kent"s Test'), UPDATE_DATE = NOW() where PRACTICE_ID = 1 AND CATEGORY_ID IN (1)
This works perfect when I explictily fire this query in db. but by using hibernate's session.createQuery(-- my query --) if thwows an error QueryTranslatorException.
Database : Mysql 5.3
Have any one faced this issue?
Thanks in advance.
Try to run this in Hibernate as native SQL query:
session.createSQLQuery(-- query text --);
Because if you use
session.createQuery(-- query text --);
Hibernate will try to execute it as HQL query which differs from usual SQL query.
HQL is object oriented query language. It operates in terms of objects rather then in terms of tables. Here posted a brief description of difference between SQL and HQL. But if you have time better to read appropriate sections of hibernate's documentation about HQL and Native SQL usage.
If you want to execute SQL Query in hibernate, Use : session.createSQLQuery(String query);