JPA: Equivalent for Oracle's REGEXP_SUBSTR - java

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)

Related

Get native SQL string from JPAQuery

I'm using Spring Data JPA + QueryDSL. I create my dynamic queries like this:
JPAQuery<Foo> query = jpaQueryFactory.select(...);
I have found this old article that shows how to retrieve programatically the native sql string: https://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/ but it doesn't work for me.
I have tried this:
String queryString1 = query.createQuery().unwrap(org.hibernate.query.Query.class).getQueryString();
String queryString2 = query.createQuery().unwrap(org.eclipse.persistence.jpa.JpaQuery.class).getDatabaseQuery().getSQLString();
The first doesn't returns me the the native sql but the JPQL string and the second fails to unrwap org.hibernate.query.internal.QueryImpl to org.eclipse.persistence.jpa.JpaQuery.
PS: I've tested before and after fetching the query.
If you need native SQL query generated by Querydsl, then you need to use SQLQueryFactory instead JPAQueryFactory. Your JPQL query returned by JPAQueryFactory is transformed to SQL by JPA not by Querydsl.

How to enable the MySQL BINARY operator in Hibernate queries and Session.get()

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.

Get dynamic SQL column names from Hibernate

I have an Oracle table that has a CLOB in it. Inside this CLOB can be a SQL statement. This can be changed at any time.
I am currently trying to dynamically run these SQL statements and return the column names and data back. This is to be used to dynamically create a table on the web page.
Using Hibernate, I create the query and get the data like so:
List<Object[]> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();
This gets the data I need, but not the column names. I have tried using the getReturnAliases() method, but it throws an error that the "java.lang.UnsupportedOperationException: SQL queries do not currently support returning aliases"
So my question is: Is there a way through Hibernate to get these values dynamically?
You can use :
q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();
to get column names in createSQLQuery.
For more details please refer to this question.
You can use the addScalar method to define the columns.
Look at 16.1.1
https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html
You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.
In 2018 I would suggest using NativeQueryTupleTransformer with native queries.
query.setResultTransformer(new NativeQueryTupleTransformer());
The result format is List<Tuple>. This format is very convenient to work with native SQL queries.

Hibernate multiple native SQL statements

I want to run a native SQL from a file using Hibernate. The SQL can contain several statements creating the database structure (i.e. tables, constraints but no insert/update/delete statements).
Example, very simple query is below (which contains the following two SQL statements)
CREATE DATABASE test;
CREATE TABLE test.testtbl( id int(5));
I am using MySQL db, and when I run the above query I am gettng syntax error returned. When I run them one by one, its ok.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'CREATE TABLE test.testtbl( id int(5))' at line 1
The code to run the query is below (above statement is assigned to 'sql' variable):
session = sf.openSession();
session.beginTransaction();
Query qry = session.createSQLQuery(sql);
qry.executeUpdate();
session.getTransaction().commit();
Any help would be appreciated.
As others have explained
You must run these queries one by one.
The hibernate code gets translated into running one update statement on JDBC.
But you provided two update statements.
In addition,
I personally prefer to have the code that creates tables outside of the Java application, in some DB scripts.
The parameters of the method createSQLQuery is t-sql code;
t-sql code to ensure that in the mysql interface analyzer correctly.
You can try changed the sql :'CREATE TABLE testtbl(id int(5));'
by the way you can use JDBC Connection api (Don't recommend to do so)
Such as:
java.sql.Connection conn=session.connection();

Hibernate session.createQuery error while replace method with single quote

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&quot;s&#32;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);

Categories