I using jpa and using jpa custom query excute method. I using TypedQuery and Entity manage excute when i write query. My Jpa excute look like :
String query = "select s from Student s where (b.beginDate + b.beginTime) < CURRENT_TIMESTAMP";
But when i excute it, it wrong when i select. It differs up to 2 hours
I have 2 question:
When i excute it, CURRENT_TIMESTAMP get database time, server
containing database or server running java machine. Because I have 2
servers. A server contains a database with a Paris time zone and a
server running spring with a JDK containing a Japan time zone.
When I create it, I want format CURRENT_TIMESTAMP as 'YYYY-MM-DD HH:Mm:SS'
Please help. Thanks
Your problem has nothing todo with JPA !
In Postgresql documentation it is written
The PostgreSQL CURRENT_TIMESTAMP() function returns the current date
and time with time zone, which is the time when the transaction
starts.
The TimeStamp returned is always a data and time WITH TIME ZONE.
In your SELECT the comparison will work if you have previously saved a time in same format.
If you have a field BEGIN_DATE_TIME with correct format, no problem.
You have certainly a problem because (b.BEGINDATE + b.BEGINTIME) will not produce a date and time with time zone value !
Can you give us more information about these 2 fields ?
Related
I have entities with a validTill field of type Instant, that is a datetime value in utc, to indicate whether the entity has been soft deleted. I would like to query for all entities, but exclude the soft deleted ones from the result. I tried something like this:
#Query(
"SELECT e " +
"FROM MyEntity e " +
"WHERE e.validTill IS NULL OR e.validTill > CURRENT_TIMESTAMP"
)
Set<MyEntity> findValidMyEntities();
However, if my local time is 2022-04-26 10:00:00.000000+03 (meaning utc time is 2022-04-26 07:00:00.000000+00) and e.validTill value is 2022-04-26 08:00:00.000000+00, the entity is not included in the result, even though e.validTill value is after the utc time. Entities with validTill after my local time are included in the result. So, I'm quite sure the issue is with the CURRENT_TIMESTAMP being transformed. How can I fix this so that the e.validTill > CURRENT_TIMESTAMP only uses the utc format values?
I also know I could give the Instant.now() value as a parameter to be used instead of current_timestamp, but I would really like to avoid that.
EDIT a day later
I have not figured out the solution, but I feel I have come closer, and thought the things I have learned might help somebody.
The problem definetly is with using CURRENT_TIMESTAMP. I made sure that in my Spring Boot project application.yml file contains spring.jpa.hibernate.jdbc.time_zone: UTC. Additionally, I added a script to my version control, so that when the database is created, the db server uses UTC timezone:
ALTER DATABASE myDatabase SET TIME ZONE TO 'UTC'
I was sure that these changes would solve my problem, but I was wrong. If I make a query for the timezone of my database using SHOW timezone, I get the response UTC. However, if I make the same query through my API, I get my local timezone as a response. So, I suspect JDBC is the culprit here, and I have not been able to find the solution.
I have a mySQL db with columns of the time type. I'm using Hibernate for ORM. The Hibernate time type is java.sql.Time. I'm noticing that whenever I update a column in the table, the time value also gets updated. The UTC offset gets added for every update. For example, consider a table with a name(string) and a time field. Let the initial db entry for time be "00:00:00". Now if I update name using an endpoint + Hibernate query, the time value gets updated to "05:00:00" in the db. If I update name again, the time value becomes "10:00:00" in the db. My time zone is EST by the way. Why is this happening and how can I prevent it?
Setting hibernate.jdbc.time_zone: UTC fixes the problem but I'd like some more information behind this behavior.
Have you Tried tot User LocalTime instead of java.sql.Time?
LocalTime should handle all the TimeZone pitfalls foe you.
Java.sql.Time is not TimeZone aware.
My mySQL server timezone is set to UTC. I have a Time column in the mySQL db which stores times in the format hh:mm:ss. I use Hibernate to access my database. When I insert a time (java.sql.Time) through a Hibernate query, it automatically gets converted to UTC. For example, my timezone is EDT so the time stored in the db gets is input time + 5 hours. How can I store the time as is? Do I have to store it as a string? I don't want to change any global hibernate properties.
We have a GUI application which sends around 40 parameters to the DB.
Based on these parameters a dynamic query is generated to fetch data. Number of fields that needs to be selected remains the same, only where condition differs.
Hibernate criteria API is used to form dynamic queries. We are using SQLMX as the database running on HP Nonstop server.
Certain dates need to be stored as 0001-01-01. Now, while selecting this field (DATE datatype), application displays this as 2001-01-01. When query is run from RazorSQL also, we are seeing incorrect results.
I could resolve this issue in RazorSQL by doing "select (cast(date1 as timestamp) from table". This gives date correctly, but with timestamp. This is OK.
In Java application, I suggested project team to use NamedNativeQueries wherein CAST as TIMESTAMP construct is specified in the query itself. Later on , I came to know that team cannot used NamedNativeQueries since queries are dynamic in nature. (Where condition can have 1 or 5 or no clauses).
Tried using Temporal.DATE annotation. Did not work. What i observed is that, we need to cast the date to timestamp while selecting itself. setMethod for the variable gets called after query has happened and if we do not do cast while doing select, value of 0001-01-01 gets changed to 2001-01-01.
Is there any way we can apply SQL constructs (similar to_date in Oracle ) before the query is executed?
User inputs date from JSP page, and it converts to Joda DateTime, the string output is
2014-03-26T00:00:00.000+09:00
However when I persist this entity containing date filed in database, and retrieve and print out again, it becomes
2014-03-25T09:00:00.000+09:00.
I don't know why database make this change to minus one day.
I use postgres, hibernate JPA for application development.
Thanks in advance.
What is the value in the database? Use pgAdmin app, the psql command line tool, or some other database admin tool to query Postgres directly.
What data type are you using in Postgres? You probably should be using TIMESTAMP WITH TIME ZONE. Avoid using the WITHOUT time zone type as it ignores any time zone offset info you may provide.
Despite the name, neither type stores any time zone info. The difference is whether you want Postgres to pay any attention to time zone info on incoming data. Be sure to read the doc thoroughly and play with it to experiment until understand how date-time works.
Read Always Use TIMESTAMP WITH TIME ZONE by David E. Wheeler, a Postgres expert.