gpt4 book ai didi

java - 使用 hibernate 仅节省数据库时间

转载 作者:行者123 更新时间:2023-11-30 02:54:51 26 4
gpt4 key购买 nike

我使用 Hibernate 并且只想在数据库中保存时间,因此我将字段声明为日期并使用时间类型时间对其进行注释,但它会引发有关格式的错误。

型号

@Entity
@Table(name = "working_policy")
public class OrganizationWorkingPolicy {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name= "policy_id")
private long policyId;

@NotEmpty
@NotNull
@Column(name= "day")
private String day;

@DateTimeFormat(pattern="hh:mm:ss" )
@Temporal(TemporalType.TIME)
@Column(name = "start_time")
private Date startTime;

@DateTimeFormat(pattern="HH:mm:ss" )
@Temporal(TemporalType.TIME)
@Column(name = "end_time")
private Date endTime;

// getter and setters
}

错误

Servlet.service() for servlet [dispatcher] in context with path [/AppointmentSchedular] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Can not construct instance of java.util.Date from String value '12:20:00': not a valid representation (error: Failed to parse Date value '12:20:00': Can not parse date "12:20:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: N/A; line: -1, column: -1] (through reference chain: com.appoitment.schedular.model.OrganizationWorkingPolicy["startTime"])] with root cause
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '12:20:00': not a valid representation (error: Failed to parse Date value '12:20:00': Can not parse date "12:20:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: N/A; line: -1, column: -1] (through reference chain: com.appoitment.schedular.model.OrganizationWorkingPolicy["startTime"])

最佳答案

尝试在 Hibernate 实体类中使用 java.sql.Time; 而不是 import java.util.Date;,它应该可以工作。

关于java - 使用 hibernate 仅节省数据库时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com