使用hibernate只在数据库中保存时间
我使用Hibernate并且只想在数据库中保存时间,因此我将字段声明为Date并使用临时类型时间对其进行注释,但它会引发关于格式的错误。
模型
@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"])
尝试使用java.sql.Time;
而不是import java.util.Date;
在Hibernate实体类中,它应该可以工作。
- 使用SequenceGenerator的javax.persistence.EntityExistsException
- 通常的hibernate性能陷阱
- Hibernate命名查询顺序依据参数
- java.lang.ClassCastException:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType
- 与hibernate的父/子关系出现问题
- 获取事务已在另一个线程exception中回滚
- 如何从具有IN和OUT参数的Hibernate调用存储过程
- 使用Spring进行Hibernate二级缓存
- 数据库约束违规不会在Hibernate中抛出exception