TemporalTipe.Time parameter in jpql - jpa

I want filter by Time in jpql but I think that I´m not doing well.
SELECT e FROM Pedido e WHERE e.fechaEntrega = :fechaInicio AND e.horaEntrega < :horaEntrega and que.setParameter("horaEntrega", horaEntrega, TemporalType.TIME); but when i see return this not filter by horaEntrega. I'm using eclipselink 2.5 any idea???
I tryed use SELECT e FROM Pedido e WHERE e.fechaEntrega = :fechaInicio AND CAST(e.horaEntrega AS TIMESTAMP) < :horaEntrega and doesn´t work and if i try cast to Time says me that expected NUMBER and got DATE
It´s weird when I write SELECT in sql im using cast(cast(etretst as timestamp) as time) < '08:00:00' and this works fine. And when I write this say me that expected TIME not a DATE

I resolved SELECT SELECT e FROM Pedido e WHERE e.fechaPedido = :fechaInicio AND CAST(CAST(e.horaPedido AS TIMESTAMP) AS TIME) < :horaZona and parameter is que.setParameter("horaZona", new Time(horaPedido.getTime()).toString());

Related

Why does the gorm postgresql throws pq: syntax error at or near ")"?

SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
end_time < ?`
query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime)
var v = request.GetVIds()
if len(v) > 0 {
query = query.Where(` v_id IN (?) `, v)
} //Only this block introduces first ) after end_time
var c = request.GetStatus().String()
if len(c) > 0 {
query = query.Where( " status = ? ", c) // this introduces the other opening brace //after AND
}
Following is the query generated and found in logs
SELECT * FROM events WHERE c_id = 1 AND start_time > '2020-04-16 18:42:00' and
end_time < '2020-04-16 18:45:50' ) AND ( v_id IN (1,2)) AND ( status = 'STATUS_MIDDLE_CLASS' ORDER BY start_time DESC LIMIT 5 OFFSET 1
The other solution in stackoverflow and internet article doesn't help.
PS: Is it because I mix db.Raw( ) and query.Where() ?
Changing ? to $1 doesn't fix the issue.
Basically a few things fixed the issue.
1) Mixing Raw and query.Where was one issue.
After making the Raw query to sr.db.Where
2)
SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
end_time < ?`
already has select * from. And then using query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime) introduces nested select *.
So, changed SELECT_QUERY as follows
SELECT_QUERY = `events WHERE c_id = ? AND start_time > ? and
end_time < ?`
solved the issue.
I found a workaround to the error which I received when I tried to add a timestamp filed in Go/Gorm solution with PostgreSQL equivalent to default: now() which is default: time.Now().Format(time.RFC3339)
I received the error because I use AutoMigrate() to create the tables in PostgreSQL. The one problem I found is when trying to use the default value of a function instead of a string (which one can use for a fixed timestamp).
So I had to go into DataGrid (since I use JetBrains, but you can use any PostgreSQL admin tool like pgAdmin) and manually add the timestamp field with default of now() or merely update the existing field to add the default of now(). Then the error goes away when doing your next build in Go.

Convert H2 sql query to JPQL

I'm using H2 database in developpement, i wrote a native query, that supports only H2. I want now to convert it to JPQL, so i can use it in production mode.
Inside the query i'm using the DATE_ADD function, which adds a value from the database to the current date, i have tried to search the equivalent for JPQL, but i wasn't successful.
#Modifying
#Transactional
#Query(
value = "UPDATE ORDER_TABLE O SET O.STATE='CANCELED' WHERE O.STATE='PENDING' AND DATEADD('HOUR',SELECT P.VALUE FROM PARAMETER P WHERE P.NAME ='PENDING_ORDER_TTL' , O.CREATED_AT) < NOW()",
nativeQuery = true)
void updatePendingOrder();
You need to calculate the Date in Java:
#Query(value = "UPDATE Order o SET o.state='CANCELED'" +
" WHERE o.state='PENDING' AND o.createdAt < :cutOff")
#Modifying
//Date/LocalDate/LocalDateTime or whatever
public void updateOrders(#Param("cutOff") Date cutOff)

Symfony DQL Postgresql group by isnt working

I have just switched from mysql to postgresql and my working dql queries stopped working.
Here is my dql
SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Like l WITH p = l.photo
WHERE p.isModerated = :isModerated
AND p.isActive = :isActive
AND p.category IN(:categories)
AND p.creationDate <= :begin
AND p.creationDate >= :end
GROUP BY p.id
ORDER BY l.creationDate DESC
Getting the next error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "l1_.creation_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... p0_.creation_date >= $4 GROUP BY p0_.id ORDER BY l1_.creati...
As i can understand, it says that group by column should be in SELECT. I dont it to be there. I need to select just p (AppBundle:Photo) and nothing more. What should be edited in dql to get it working properly?
Thank you.
I just replaced ORDER BY l.creationDate DESC to ORDER BY p.creationDate DESC
It will work for me. As i understood, ORDER BY column should be SELECTed

Error using CURRENT_TIMESTAMP() into a query

I want to compare two date in jpql query using the current date function
I got an error
Syntax error parsing [SELECT d FROM Dossier d WHERE d.depid=1 AND d.typeDossier = :tpd AND d.dateCreation < CURRENT_TIMESTAMP() + 5].
[105, 107] The left expression is not an arithmetic expression
This is my query:
public List<Dossier> getDossierFindAllParDepartementDBTECHandUrgen() {
return (List<Dossier>) em.createQuery("SELECT d FROM Dossier d WHERE d.depid=1 AND d.typeDossier = :tpd AND " +
"d.dateCreation < CURRENT_TIMESTAMP() + 5",
Dossier.class).setParameter("tpd", "Urgent").getResultList();
}
JPA supports function CURRENT_TIMESTAMP
https://en.wikibooks.org/wiki/Java_Persistence/JPQL#Functions
but will not work with arithmetic operations. You can solve the problem by using a parameter, for example
TypedQuery<Dossier> query = em.createQuery("SELECT d FROM Dossier d WHERE d.depid=1 AND d.typeDossier = :tpd AND " +
"d.dateCreation < :fiveDaysAhead",
Dossier.class);
Date myFiveDaysAhead = new Date(Calendar.getInstance().add(Calendar.DAYS_OF_YEAR,5).getTimeInMillis());//or something
query.setParameter("tpd", "Urgent");
query.setParameter("fiveDaysAhead", myFiveDaysAhead, TemporalType.TIMESTAMP);
It may also be possible to find vendor specific solutions, as i noticed in one other answer https://stackoverflow.com/a/18514326/2835455

OpenJPA String as date attribute

I have a Query to parse on JPA , some errors have happened . I want a right way to make this.
Original:
Query q = this.em.createQuery("SELECT e FROM Entity e WHERE e.codigo = :codigo ORDER BY e.data ASC");
My solution but doesn't work:
Query q = this.em.createQuery("SELECT e FROM Entity e WHERE e.codigo = :codigo ORDER BY to_date(e.data,'DD/MM/YYYY') ASC");
where e.data is a String like "01/01/2014"
Solved
I´d replaced it with a nativeQuery.
Then a had be able to use Oracle Date Function