使用hibernate只保存数据库中的时间 [英] Save only time in database using hibernate
本文介绍了使用hibernate只保存数据库中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Hibernate并希望仅在数据库中保存时间,所以我将field声明为Date并用临时类型时间对其进行注释,但它引发了有关格式的错误。
$ b
@Entity
@Table(name =working_policy)
public class OrganizationWorkingPolicy {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =policy_id)
private long策略ID;
@NotEmpty
@NotNull
@Column(name =day)
private String day;
@DateTimeFormat(pattern =hh:mm:ss)
@Temporal(TemporalType.TIME)
@Column(name =start_time)
private日期startTime;
@DateTimeFormat(pattern =HH:mm:ss)
@Temporal(TemporalType.TIME)
@Column(name =end_time)
private日期endTime;
// getter and setters
}
错误
Servlet.service()用于path [/ AppointmentSchedular]上下文中的servlet [dispatcher]请求处理失败;嵌套异常是java.lang.IllegalArgumentException:无法从字符串值'12:20:00'构造java.util.Date实例:不是有效表示(错误:未能解析日期值'12:20:00':无法解析日期12:20:00:与任何标准格式不兼容(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](通过引用链:com.appoitment.schedular.model.OrganizationWorkingPolicy [startTime])],其根源为
com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串值'12:20:00'构造java.util.Date的实例:不是有效表示(错误:无法解析日期值'12:20:00':无法解析日期12:20: 00:不符合任何标准格式(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](通过引用链:com.appoitment.schedular.model.OrganizationWorkingPolicy [startTime])
解决方案使用
java.sql.Time;
代替 import java .util.Date;
在Hibernate实体类中,它应该有效。 I use Hibernate and want to save only time in database, so I declare field as a Date and annotate it with temporal type time, but it throws me error regarding format.
model
@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
}
Error
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"])
解决方案
Try using java.sql.Time;
instead of import java.util.Date;
in the Hibernate entity class, it should work.
这篇关于使用hibernate只保存数据库中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文