H2 limitations with Sysdate and hence cant use in functional test cases - oracle

I am using Spring Boot and Spring Data JPA. In my application application is running over Oracle DB and Functional Test cases against the H2 DB.
I've to check if key is expired or not and its a Date field in DB. So Sysdate< ExpiryDate and with Oracle this works fine, but sysdate is not a function in H2 and I simply cant keep two queries 1 for Oracle and Other for H2.
#Query("SELECT new com......Test( "
+ "p.id,p.keyId, p.keyType, p.expiryDate...., sysdate) "
+ "FROM Table A p "
+ "WHERE ......")
List<EnableEncryptionKeyProjection> findBySrcClientId(....);

You could use current_timestamp for both.

Related

How to prevent adding default table alias in hibernate #Joinformula

In my application I am using the following #JoinFormula
#JoinFormula(value="(select s.\"Id\" from \"Student\" s where ash.\"Class_Id\" = id)" )
However, at runtime the generated query is:
(
select
s.cla0_."Id"
from
cla0_."Student" s
where
s.cla0_."Class_Id" = cla0_.id) as formula4_0_,
I need to prevent adding cla0_ table alias to the formula query. How could it be done in hibernate?
I am using it in spring boot, and postgres as database.

How to use getDate() in h2 as like sql server

I'm testing the already developed application through Junit test cases, uses SQL server in production but while executing test cases we used embedded h2 database.
And some of the SQL server specifications are not working in h2 (ex: conditional queries, getDate(), DATEPART,..........) can anyone help me how to tackle this kind issues further in my JUnit testing with h2 DB
SQL SERVER: select * from getDate() -: 2006-12-30 00:38:54.840
h2 DB : select * from getDate() -: 2006-12-30
While executing test cases that scripts are executing in h2 database and as a part of logic, I'm parsing the value from getdate() into SimpleDate format of 2006-12-30 00:38:54.840, and I'm getting parse exception that 2006-12-30 couldn't parse.
Thanks in Advance
To use getDate in H2, simply run the following query before using a query with getDate()
drop alias if exists getDate;
create alias getDate as '
java.util.Date getDate() {
return new java.util.Date();
}
';
You can use User-Defined Functions and Stored Procedures built in H2 database.

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.

JPA Cast is not working in Oracle

I have a table person{id,personName,birthDate}. In the table I have to get all the person details by their birthDate with out comparing its time. I have the following query
Query q = entitymanager.createQuery(select p from person p where
CAST(p.birthDate as date) = :birthdate);
q.setparameter("birthdate",new Date(),TemporalType.DATE) ;
When I test the above query using mysql, it gives the correct result. But it is not functioning correctly in Oracle. I tried the between...and logic by setting the starting time as 0:0:0 and the ending time as 23:59:59. Is there any other alternative.
I am using JPA 1.0 with hibernate as a service provider. Thanks in advance for your help.

How do I use the current date in an HQL query with an Oracle database?

I'm trying to write this query using Hibernate 3 and Oracle 10.
from Alert alert
where alert.expiration > current_date()
order by alert.priority, alert.updated, alert.name
It's creating SQL like this -
Hibernate: select alert0_.ANNOUNCEMENTS_ID as ANNOUNCE1_1_, alert0_.ANNOUNCEMENT
S_NAME as ANNOUNCE2_1_, alert0_.ANNOUNCEMENTS_PRIORITY as ANNOUNCE3_1_, alert0_.
ANNOUNCEMENTS_EXPIRATION as ANNOUNCE4_1_, alert0_.ANNOUNCEMENTS_UPDATE_DATE as A
NNOUNCE5_1_ from NYC311_ANNOUNCEMENTS alert0_ where (alert0_.ANNOUNCEMENTS_EXPIR
ATION>current_date()) order by alert0_.ANNOUNCEMENTS_PRIORITY , alert0_.ANNOUNC
EMENTS_UPDATE_DATE , alert0_.ANNOUNCEMENTS_NAME
I'm getting all of these wacky errors like "missing right parenthesis" when there is apparently perfectly balanced parenthesis.
Why is Oracle freaking out at this? Is there a better way to write my HQL query?
Shouldn't it be current_date?
Hibernate will translate it to the proper dialect.
I did not find a real "Hibernate will translate this to that" reference documentation, but the expression, in general, can be found in HQL Expressions for Hibernate 4.3.
Then there is the Java Persistence API 2.0 (JPA) specification which defines expressions for the Java Persistence query language (JPQL) and their meaning e.g. for current_date:
4.6.17.2.3 Datetime Functions functions_returning_datetime:= CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP The datetime functions
return the value of current date, time, and timestamp on the database
server.
Is current_date() a Hibernate function?
I would use sysdate instead. like this:
where alert.expiration > sysdate
Or to ignore time of day:
where alert.expiration > trunc(sysdate)

Resources