小编典典

Joda Time-Hibernate将昨天的日期插入数据库

hibernate

我现在有一个奇怪的问题。当我将今天的日期插入数据库时​​,它映射到昨天的日期,它随每个日期发生。例如,当我尝试插入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 im版本使用: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

所以日期错误

发现类似的线程,但并没有真正的帮助:我一直在使用两天的日期jdbc.driverClassName=com.mysql.cj.jdbc.Driver


阅读 208

收藏
2020-06-20

共1个答案

小编典典

好了,我设法解决了这个问题,我从以下位置更改了jdbc网址:

jdbc:mysql://localhost:3306/ElectronicsStoreDB?useUnicode=tr‌​ue&useJDBCCompliantT‌​imezoneShift=true&us‌​eLegacyDatetimeCode=‌​false&serverTimezone‌​=UTC

jdbc:mysql://localhost:3306/ElectronicsStoreDB?useTimezone=trueuseUnicode=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Warsaw

令人惊讶的是,它现在应该可以正常工作

2020-06-20