spring data jpa native query pagination for SQLServer - spring

I am using Spring 4.x and hibernate 5.x with SQLServer. I have a requirement to paginate data returned by a nativequery. I'm doing it like this:
#Query(value="select * from X where y=?1 \n#pageable\n",
countQuery="select count(*) from X where y=?1 \n#pageable\n",nativeQuery=true)
List<Object[]> XXX(Integer userId, Pageable pageable);
It's throwing me com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '#pageable'. If I don't use \n#pageable\n then it gives me compile time error that #pageable can't be used with native query. Please help if anyone faced similar issue

Related

How to seed rand() in Spring Boot JPA query

In a Spring Boot JPA query, when adding a seed to function 'rand' I'm getting "org.hibernate.QueryException: function takes no arguments: rand"
#Query("select mt from MyTable mt order by rand(4711)")
My database supports both rand functions, with and without parameter.
Is there a way to make that JPA query work?

Spring boot #Query ignores #Table on view call

I have a spring boot 2 application that works great with entities that are mapped to a table that is in a MariaDB. I now do a view call and I have mapped that entity to the view using the #Table(name="ViewExpiredAccounts") annotation and now when the method is called the table name is ignored. I have a JpaRepository with this method:
#Query(value = "SELECT v FROM ViewExpiredAccounts v")
List<ViewExpiredAccount> expiredAccounts();
When I call this method I get an error:
Table 'view_expired_accounts' doesn't exist
The query SHOULD! translate the table name so that the eventual SQL query sent to MariaDB is: SELECT * from ViewExpiredAccount however it doesnt do that. Is this a bug in Spring??
did you reset the hibernate naming strategy?
if yes, the following property might help
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Hibernate/Spring boot jpa on Impala/kudu with cloudera jdbc driver

I have an API in spring boot using hibernate.
Initially, the database to request was Hive, it's now Kudu throw Impala.
I followed recommendations and set the dialect to org.hibernate.dialect.HSQLDialect.
The classical requests work fine excepting the Page<T> findAll(#Nullable Specification<T> var1, Pageable var2) from org.springframework.data.jpa.repository
when I paginate (page > 0) and so I have the exception:
[Cloudera]ImpalaJDBCDriver ERROR processing query/statement. Error Code: 0, SQL state: TStatus(statusCode:ERROR_STATUS, sqlState:HY000, errorMessage:ParseException: Syntax error in line 1:
select limit 10 10 qualityres0_.id ...
^
Encountered: LIMIT
Expected: ALL, CASE, CAST, DATE, DEFAULT, DISTINCT, EXISTS, FALSE, IF, INTERVAL, LEFT, NOT, NULL, REPLACE, RIGHT, STRAIGHT_JOIN, TRUNCATE, TRUE, IDENTIFIER
Spring boot version: 2.3.4.RELEASE
Impala jdbc driver: com.cloudera.impala.jdbc ImpalaJDBC41 v2.6.15.1017
Finally it works fine with org.hibernate.dialect.SQLServerDialect

How to do cross database join using spring jdbc

I have a table A in db1 and table B in db2. I wanted to do a join between db1.A and db2.B and get the result.
The sql query for this in Microsoft SQL server would be something like this
select a.name from db1.A a
join db2..B b on
a.id = b.id
Now, I am not sure how would I perform the above query using springJdbcTemple. I have setup the datasource and sping jdbc template for db1.A as templateA and templateB respectively.
Do I need a global transaction manager like XA to perform this query even though its just a simple join?
I am using Spring 3.1.X version.
Note:
I have setup the datasource and have simpleJdbcTemple for the datasource with me, but if I run this query using simpleJdbcTemplate I get the bad sql grammer exception stating that table b is not recognised which makes sense as the simpleJdbcTemplate is accesing db1 and not db2.

Spring embedded DB schema not found issue

I am using embedded HSQLDB in my JUnits. It is throwing
SQL state [3F000]; error code [-4850]; invalid schema name:info
when it executes query following query
select db.id, db.name,info.desc from user_db db,user_info info where db.user_id = info.user_id and info.active = 'Y' and db.dept = info.dept(+)
the above query runs fine in oracle but not in embedded HSQLDB
please let me know what is wrong in this.

Resources