Reputation: 135
I'm having a weird problem right now. When I insert todays date into database, it's mapped into yesterdays date, it happens with every date. For example, when I try to insert 2016-09-02, database saves it as 2016-09-01. Here is my mapping:
@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(name = "OrderDate", nullable = false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private final LocalDate orderDate;
I create instances by new LocalDate()
I tried printing the value on console right before it gets saved into database and it prints it correctly, but the value in database is from the day before :/
So the problem is either with hibernate mapping, or with mysql.
I save the date into database in this method:
@Autowired
private SessionFactory sessionFactory;
public void persist(T entity) {
getSession().persist(entity);
}
I don't even know if it's a problem with hibernate, or mysql. This is really weird. Can you give me any pointers what could be wrong? Thanks in advance The version of mysql im using: 5.7.14-log
Edit: when i manually insert data in mysql like this:
insert into Orders values (149,14,'2016-09-03','2016-09-03',199.99)
the date is saved correctly
My sql settings: http://pastebin.com/87d6pkmE
I enabled logging in mysql, and code processed by mysql is:
2016-09-03T19:54:06.016215Z 125 Connect root@localhost on ElectronicsStoreDB using TCP/IP
2016-09-03T19:54:06.017215Z 125 Query /* mysql-connector-java-6.0.3 ( Revision: 9fb85a76ccb7506157e668f1516464a46e317d4a ) */SELECT @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS tx_isolation, @@wait_timeout AS wait_timeout
2016-09-03T19:54:06.018215Z 125 Query SET character_set_results = NULL
2016-09-03T19:54:06.018215Z 125 Query SET autocommit=1
2016-09-03T19:54:06.018215Z 125 Query SET autocommit=0
2016-09-03T19:54:06.021216Z 125 Query select product0_.ProductID as ProductI1_4_0_, product0_.Category as Category2_4_0_, product0_.Description as Descript3_4_0_, product0_.Discontinued as Disconti4_4_0_, product0_.Manufacturer as Manufact5_4_0_, product0_.Name as Name6_4_0_, product0_.UnitPrice as UnitPric7_4_0_, product0_.UnitsInStock as UnitsInS8_4_0_ from Products product0_ where product0_.ProductID=19
2016-09-03T19:54:06.045217Z 125 Query insert into Orders (CustomerID, OrderDate, ShippingDate, TotalPrice) values (14, '2016-09-02', '2016-09-02', 437.99)
2016-09-03T19:54:06.048217Z 125 Query insert into OrderDetails (OrderID, ProductID, Quantity, UnitPrice) values (163, 19, 1, 437.99)
2016-09-03T19:54:06.091220Z 125 Query update Products set Category='Printer', Description='USB 2.0, Wi-Fi', Discontinued=0, Manufacturer='EPSON', Name='XP-610', UnitPrice=437.99, UnitsInStock=134 where ProductID=19
2016-09-03T19:54:06.092220Z 125 Query commit
2016-09-03T19:54:06.094220Z 125 Query SET autocommit=1
2016-09-03T19:54:06.095220Z 125 Query select @@session.tx_read_only
2016-09-03T19:54:06.095220Z 125 Quit
so its the wrong date
found similar thread but it doesn't really help:
dates consistently two days off
i'm using jdbc.driverClassName=com.mysql.cj.jdbc.Driver
Upvotes: 2
Views: 1148
Reputation: 135
Alright, I managed to fix this I changed jdbc url from:
jdbc:mysql://localhost:3306/ElectronicsStoreDB?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
to
jdbc:mysql://localhost:3306/ElectronicsStoreDB?useTimezone=trueuseUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Warsaw
And suprisingly it works as it should now
Upvotes: 4