我现在有一个奇怪的问题。当我将今天的日期插入数据库时,它被映射到昨天的日期,它发生在每个日期。例如,当我尝试插入2016-09-02时,数据库将其保存为2016-09-01。 这是我的映射:
@DateTimeFormat(pattern="dd/MM/yyyy")
@Column(name = "OrderDate", nullable = false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private final LocalDate orderDate;
我按new LocalDate()
创建实例
我尝试在控制台上将值保存到数据库之前将其打印出来并正确打印,但数据库中的值是从前一天开始的:/
所以问题是使用hibernate映射,还是使用mysql。
我用这种方法将日期保存到数据库中:
@Autowired
private SessionFactory sessionFactory;
public void persist(T entity) {
getSession().persist(entity);
}
我甚至不知道它是否是hibernate或mysql的问题。这真的很奇怪。你能告诉我什么可能是错的吗?提前致谢 使用的版本为mysql:5.7.14-log
编辑:当我手动在mysql中插入数据时:
insert into Orders values (149,14,'2016-09-03','2016-09-03',199.99)
日期保存正确
我的sql设置: http://pastebin.com/87d6pkmE
我在mysql中启用了日志记录,而mysql处理的代码是:
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
所以错误的日期
发现了类似的帖子,但它并没有真正帮助:
dates consistently two days off
我正在使用jdbc.driverClassName=com.mysql.cj.jdbc.Driver
答案 0 :(得分:2)
好吧,我设法解决了这个问题 我改变了jdbc网址:
jdbc:mysql://localhost:3306/ElectronicsStoreDB?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
到
jdbc:mysql://localhost:3306/ElectronicsStoreDB?useTimezone=trueuseUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Warsaw
令人惊讶的是它现在可以正常运作