JPA save another Time into database - java

I have encountered problem in using JPA to save Time type into database.
Time value from browser into Java is mapped correctly, but when JPA save the value into DB, it become a different value.
Is there any solution to save time into database without changing the data of time?
(I have only 2 solutions: Change Time type into long or string, but I still prefer the solution that can save Time value).
Data input in browser:
Data input in browser
Save before save into DB:
Data before save into database
The data save into database(I use MySQL). I change value from 11:00:00 to 04:00:00:
Data when save into database

Actually you are trying to use timestamp (which actually represents a time point) field for duration (which represents time interval) purposes.
When you enter 11:00 it might means 11:00 at PST, UTC, ICT timezones. And these will be absolutely different values.
Instead of java.util.Date in java you have to use java.time.Duration and use TIME field in MySQL (http://www.mysqltutorial.org/mysql-time/)

Related

Hibernate is adding UTC offset for every db update

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.

How to store values of mySQL Time data type using Hibernate without conversion to mySQL server timezone?

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.

Date is changed after persist in database?

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.

How to keep date info?

In java, I want to know that what is the best practice to keep date info for display, query, report etc. It seems that if we persist as long, all timezone dependency will be removed and we will keep 'persist globally, display locally' principle since Date object automatically converts long to current timezone.
But what is the advantage of persisting as Date object?
Do I loose any info other than info owner's timezone?
Can I get any wrong info when DLS takes into account?
Difference between persisting as UTC date and long is just readable db info?
Depending on your database you should use either TIMESTAMP WITH TIMEZONE or you convert it to UTC time and store it as long.
The first one relies on the DB to handle it correctly (the DB will, but will your DB driver? You have to test this for your setup). The second one makes it a manual process, you will get the correct result in the end but will have more hazzle with it because you have to take care about everything.
Inside Java you might want to use Calendar over Date because there you can specify the TIMEZONE etc. manually, thus you are able to display Dates in timezones different to your own easier.

Should I store the timezone separately from the timestamp for Postgres and JDBC?

It seems (and maybe I'm wrong) that if you want to preserve the timezone of when something happened with JDBC and Postgres you need to store the timezone separately from the timestamp.
That is I would prefer to give my ORM/JDBC/JPA a Java Calendar (or Joda DataTime) with say timezone America/New_York to a Postgres timestampz field. AND I would expect on retrieval regardless of the Servers timezone (or defaulting to UTC) to give me back a Calendar with timezone America/New_York. But just looking at most JDBC code (and things that depend on it that doesn't happen).
Is this correct?
This seems ridiculous that I would need to store the tz in another field when postgres supports it.
Thus it seems like the only two options are:
Select the timestampz Postgres column as a java.util.String and parse it.
Store the timezone as a separate field.
Option number one and two one would require some sort of conversion interceptors for my SQL mapping / ORM libraries.
What is the best solution for JDBC ?
What is the best solution for JPA (if different than JDBC)?
When you store a timestamp with time zone (timestamptz) it's converted to UTC for storage in the DB. When retrieved, it's converted to the client's current timezone, not the timezone it was originally in. It's a point in time, basically.
There is also timestamp without time zone (timestamp). This is not subject to conversion, but does not carry a timestamp with it. If you store a timestamp with your client time zone set to UTC, then retrieve it when the client time zone is '+08:00', you get the same value. That's half what you want, in that it preserves the raw time value.
The names and behaviours are awful and confusing, but set by the SQL standard.
You must store the time zone separately if you wish to record a point in time at a particular time zone. I'd recommend storing it as an INTERVAL with a CHECK constraint limiting it to be colname BETWEEN INTERVAL '-12' HOUR + INTERVAL '1' SECOND AND INTERVAL '12' HOUR. That definition rejects -12:00 and accepts +12:00; I'm not totally sure that's right, so check.
You could either store the timestamp of local time at that time zone (what I'd probably do), or store the timestamptz of the UTC time when the event occurred plus an offset that lets you convert it to local time.
Either will work fine for JDBC. For JPA, it'll depend on how well your provider understands and maps interval types. Ideally you want a transient generated field in your entity that reconstructs the Calendar instance you want using the timestamp and interval stored in the database.
EclipseLink supports storing the timezone in Oracle, I think you could get it to be stored in Postgres as well if you customized your PostgreSQLPlatform.

Categories

Resources