java - 更新时记录的日期时间被设置为向后 3 小时

标签 java mysql spring hibernate

我正在 Spring boot 和 Hibernate 中开发 CRUD 应用程序。我有一个名为 Order 的表。该表有两列,分别为 status(整数)和 datetime(类型为 mysql datetime)。在我的应用程序中,我定期更新其中一行中的 status 列的值。

当我这样做时,会发生一些奇怪的事情。该记录的 status 列已更新,但是,datetime 列也正在更新,我什至没有指示。每次更新一行后,datetime 都会设置为比之前的值早 3 小时。

我正在 Java 的 Spring Boot 和 Spring Boot JPA MySQL 中开发此应用程序。我正在使用 JPA 存储库。这是进行更新的片段:

public String incrementOrderStatus(Long orderId) {
    Order order = orderRepository.findOne(orderId);
    OrderState nextState = order.getState().next();
    order.setState(nextState);
    orderRepository.save(order);
    return nextState.toString();
}

订单具有以下属性:

@Data
@Entity
@Table(name = "`order`")
@NoArgsConstructor
@AllArgsConstructor
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long id;

    private String name;
    private Calendar datetime;

    @Enumerated(EnumType.ORDINAL)
    private OrderState state;

    private String address;
    private String phoneNumber;
    private String email;

    @Enumerated(EnumType.ORDINAL)
    private OrderSource source;

    private Float totalPrice;

    @ManyToOne(optional = true)
    private Offer offerUsed;

    private String notes;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "order", fetch = FetchType.LAZY)
    private List<MealOrderDescriptor> meals;

    public Order(CustomOrderView orderView) {
        this.name = orderView.getName();
        this.datetime = Calendar.getInstance(TimeZone.getTimeZone("Europe/Istanbul"));
        this.state = OrderState.PENDING;
        this.address = orderView.getAddress();
        this.phoneNumber = orderView.getPhoneNumber();
        this.source = orderView.getSource();
        this.totalPrice = orderView.getTotalPrice();
        this.notes = orderView.getNotes();
    }
}

在 JDBC 查询字符串中,我还将时区设置为我的时区,即 serverTimezone=Europe/Istanbul。我使用的 MySQL 版本为 5.7.19,JDBC 版本为 6.0.6,Java 1.8 和 Spring Boot 1.5.9,Ubuntu 16.04.3

提前致谢。

最佳答案

即使您没有在 orderRepository.save(order); 之前调用 order.setDatetime()datetime 字段也包含收到的值通过 orderRepository.findOne(orderId)。

之后,orderRepository.save(order);语句尝试使用orderRepository.findOne(orderId)收到的值更新datetime字段>。如果与之前的值相差负 3 小时,则可能是时区问题。

我想,如果你使用TIMESTAMP类型而不是DATETIME这种情况就会消失。因为MySQL DATETIME类型不保存TimeZone信息。

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)

您可以查看official documentation .

关于java - 更新时记录的日期时间被设置为向后 3 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48612569/

相关文章:

java - 使用 Spring @RestController 和 @QuerydslPredicate 来处理 GET With ZonedDateTime 参数

java - Maven - 检测同一依赖项的多个版本

mysql - 将列分成行(数据透视表?)

MySQL 查询使用 'in' 运算符 : why different results w/quotes?

mysql - Mysql 中的 Lead 和 Lag 函数

java - 在 JSP 中验证表单

java - 转发请求到非@RequestMapping

java - 如何在java中解析MM/dd格式的日期

java - 使用 Selenium JAVA 下载在新窗口中打开的 PDF

java - 按照模板将 long 转换为字符串