2016-06-01 23 views
1

私はHibernateを使用してデータベースに時間だけ保存したいので、日付としてフィールドを宣言し、一時的な型の時間で注釈を付けます。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"]) 
+0

参照http://www.developerscrappad.com/228/java/java-ee/ejb3-jpa-dealing-with-date-time-and-timestamp/ –

答えて

2

Hibernateのエンティティクラスのjava.sql.Time;の代わりimport java.util.Date;を使用してみてください、それが動作するはずです。

関連する問題