How to run native postgres query using spring-data-jpa - postgresql

I am using spring-data-jpa to run native postgres queries. The query is performed on entity's jsonb data.
I can run the raw query on db server with success:
SELECT e.*
FROM public.entity e where e.json ? 'salary' and e.json ->> 'salary' > '10000';
But, since spring-data-jpa also supports ? for the parameterized queries, there seems to be conflict in the grammer of the query, therefore application eventually fails to even start.
#Query(value = "select e.* from Entity e where e.json ? 'salary' and e.json->> 'salary' > ?1", nativeQuery = true)
List<Lead> getEntitiesBySalaryGreaterThan(String value);
}
Please let me know the workaround or correct way of executing the intended native query in spring-data-jpa env

Is it possible that you need to escape ? as described here?

Related

Why does Spring JPA #Query return an error when I omit the table alias?

I'm querying a PostgreSQL table with a very simple query using Spring Data JPA:
#Query(value = "SELECT id FROM location l", nativeQuery = true)
This works fine. However, if I omit the table alias and use:
#Query(value = "SELECT id FROM location", nativeQuery = true)
I get an error saying No Dialect mapping for JDBC type: 1111. ID is a UUID in both PostgreSQL and the DTO.
Why does this happen? As far as I can tell, that's a perfectly valid query, and I'm not even using the table alias in the working version. Adding #Type and #TypeMappings made no difference. I checked the data I'm querying, and all the IDs are valid.
I'm happy to use an alias now that I've figured this out, but I'm curious about what's happening behind the scenes that causes this to fail.
Spring Data JPA tries to parse your query in order to derive a count query or add/modify and ORDER BY clause.
This to some extend relies on having an alias for the main table/entity of the query.
It should give you a proper error that it can't find an alias instead of the weird error you are seeing.

How to get attribute value from jsonb in postgresql using Panache and Hibernate?

I use quarkus-spring-data-jpa extension, but since native queries are not supported, I can't execute a query similar to this select * from test_table where test_json->>'FirstValue' = :value. I have not found how to do this using only JPQL. How can I make such a request using Hibernate and Panache ?

How to order by json property of Posgres in Spring boot JPA?

I wrote a #Query in JPA to fetch the details by json property which is resulting error.
#Query("SELECT t FROM Tcl order by t.equipment->>'eqpm_n' ASC")
public List<Tcl> getEquipmentList();
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token
SELECT t FROM com.target.mpe.models.Tcl order by t.equipment->>'eqpm_n' ASC
And same query is working well in Postgres console.
How can i make it work in SpringBoot JPA?
Do i need to try Native Query?
JPQL does not support this PostgreSQL syntax. You have to use a nativeQuery:
#Query(value = "SELECT * FROM Tcl t order by t.equipment->>'eqpm_n' ASC", nativeQuery = true)
public List<Tcl> getEquipmentList();

jOOQ insert into .. where not exists for Postgres

I'm attempting to do an upsert-style statement for Postgres in jOOQ. The framework I'm running in takes care of concurrency concerns in this specific situation so I'm not worried about that. I'm only using jOOQ for creating the SQL, the actual execution is done via Spring's JdbcTemplate and a BeanPropertySqlParameterSource.
I've decided to go with a two-step "insert..where not exists" / "update .." process.
My SQL is:
insert into mytable (my_id, col1, col2) select :myId,
:firstCol, :secondCol where not exists (select 1
from mytable where my_id = :myId)
I'm using Postgres 9.4, jOOQ 3.5. I'm not sure how to express both the jOOQ params in the select and the "where not exists" clause in jOOQ.
Suggestions to change programming languages or databases aren't viable in my situation.
If you want to reuse a named parameter in jOOQ, ideally, you create the AST element outside of the query, as such:
// Assuming a static import
import static org.jooq.impl.DSL.*;
Param<Integer> myId = param("myId", Integer.class);
You can then use it multiple times in your query:
using(configuration)
.insertInto(MY_TABLE, MY_TABLE.MY_ID, MY_TABLE.COL1, MY_TABLE.COL2)
.select(
select(
myId,
param("firstCol", MY_TABLE.COL1.getType()),
param("secondCol", MY_TABLE.COL2.getType())
)
.whereNotExists(
selectOne()
.from(MY_TABLE)
.where(MY_TABLE.MY_ID.eq(myId))
)
);

Can we put lockMode=LockModeType.PESSIMISTIC_WRITE in JPA delete query?

I have written a named query
#NamedQuery(name = "deleteEmployee", query = "DELETE from emp e where e.Id = :Id", lockMode=LockModeType.PESSIMISTIC_WRITE)
However Eclipse link throws following error -
java.lang.IllegalArgumentException: A lock type can only be used with a select query (which allows the database to be locked where necessary)
However it works with openJPA, and I didnt see any java docs related to above exception. Please suggest.