I'm trying to perform the following query:
SELECT p FROM Pessoa p WHERE TRANSLATE(UPPER(p.nome), 'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ', 'AAAAAEEEEIIIIOOOOOUUUUC') LIKE CONCAT('%',TRANSLATE(UPPER(?1), 'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ', 'AAAAAEEEEIIIIOOOOOUUUUC'),'%')
But i realize that hibernate is generating the following final SQL:
... where TRANSLATE(upper(pessoa0_.ds_nome), 'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ', 'AAAAAEEEEIIIIOOOOOUUUUC') like ('%'||TRANSLATE(upper(?)||'ÂÁÀÄÃÊÉÈËÎÍÌÏÔÓÒÖÕÛÚÙÜÇ'||'AAAAAEEEEIIIIOOOOOUUUUC')||'%')...
Note that the commas inside the TRANSLATE function were replaced by || which leads to an org.postgresql.util.PSQLException: ERROR: function translate(text) does not exist
What am I doing wrong?
Spring: 4.3.4.RELEASE
Spring data: spring-data-jpa:1.10.5.RELEASE
Hibernate is not simply replacing commas. It is converting a JPQL (or possibly HQL) query int SQL. The double pipes || are a common SQL syntax for the JPQL CONCAT function.
If it doesn't work for your Postgres DB you are probably using the wrong dialect. Configure Hibernate to use the correct Dialect for your database.
See for example this article for how to do that.
Related
I have a Spring Boot application with a JpaRepository which contains a native query with this structure:
SELECT a, b, c, CURSOR( SELECT ... FROM ... ), ...
FROM ...
WHERE ...
And I'm getting the following exception:
org.hibernate.MappingException: No Dialect mapping for JDBC type: -10
I am using Oracle12cDialect, and I think the problem comes from the CURSOR in my query, but I don't know how to solve it. Also, I cannot modify the query, I've been ordered to try to implement it as it is.
Thanks in advance for any help.
This SQL returns column of type CURSOR, which is a handle to another SQLs' resultSet. I do not believe SpringBoot support this.
This is a trick how to return data having more than two dimensions.
You have to either use JSON object as 4th column, or use ah-hoc query for each row for 4th column. To query that detail/child object.
We're using testcontainers in integration test with DB. To setup initial state of DB we use container method withInitScript(*pathToScript*). Under the hood it uses org.postgresql.jdbc.PgStatement class to execute query. But an executing fails if sqlQuery contains function/procedure with $$ delimiter. The solution is to replace $$ with ', but it takes long time to control sql sqript and keep in mind necessity of replacing delimiter.
There is related question, but no solution: Exception in JPA when using seed file for PostgreSQL
Is it possible to point a type of delimiter or something like that?
I'm using Eclipse link with JPA 2.1 , noting that I have an Oracle Query use function "INSTR" with the feature of occurrence param.
Is there any equivalent to this method in JPA ?
JPQL supports LOCATE function:
LOCATE
the index of the string within the string, optionally starting at a start index
I have a query starting with REPLACE. When I use it directly in MySQL console everything is fine, so query is ok. However, when I put it in my code like this:
#Query("REPLACE INTO WeekAggregate...
There is an error:
web - 2016-10-05 10:35:44,297 [localhost-startStop-1] ERROR o.h.hql.internal.ast.ErrorCounter - line 1:1: unexpected token: REPLACE
antlr.NoViableAltException: unexpected token: REPLACE
How could I fix this? Is REPLACE not supported in HQL?
HQL is trasversal to underlying DBMS. So some functions you're using in your SQL can't be interpretated by HQL.
You have two ways:
change your HQL query (in this case you can rewrite your query with an DELETE/INSERT statement)
you can write your query in SQL and you can use a method createSqlQuery so the interpreter, runs the query as SQL native query.
I changed my query to native SQL query by using nativeQuery = true flag.
When using Spring Data JPA with Hibernate, what are the options available to write queries that join with table functions.
For example, I'd like to generate queries as described below:
CASE 1: SELECT * FROM getfoo(1) AS t1;
CASE 2: SELECT * FROM getfoo(1) x INNER JOIN tbl1 y on x.id = y.id;
Edit
To elaborate more, I'm using Spring Data for all things CRUD (It works great). However, I need to write complicated queries that join tables with "table functions". Table functions(AKA Table-Valued User-Defined Functions) are database functions that return tabled-values which can be used in the JOIN clause in combination with tables. Postgresql and Sql Server support them.
In the Spring Data realm, which includes much more than JPA, what are the options to consider when writing such queries? Whats the best practice from your experience? user2658013 was kind enough to describe one such approach using the entityManager.reateNativeQuery method.
In my mind here are the options:
JPA
Use #NamedStoredProcedureQuery ( >=JPA 2.1)
Use entityManager.createNativeQuery or #NamedNativeQuery
Non-standard
Use Spring Data's #Query to declare finder queries directly on repository methods.
Use SimpleJdbcCall
Any others?
I believe you are asking about Postgres stored functions. Postgres stored functions are analogous to "Stored Procedures". So I think you are asking how would invoke a stored procedure using JPA? Am I close?
The following pseudo code is derived from details published here (see section on Stored Procures):
http://www.oracle.com/technetwork/articles/vasiliev-jpql-087123.html
EntityManager em = emf.createEntityManager();
SOMEVAL_TYPE result = (SOMEVAL_TYPE)em.createNativeQuery("SELECT getfoo(?1) FROM SOMEDB")
.setParameter(1, SOME_PARM)
.getSingleResult();
In general you can use JPQL with JPA instead of SQL.
Note! The above assumes you have already created the stored function in you Postgres database.
Hope that helps :)