SQLException:java.util.Date不能使用hibernate强制转换为java.sql.Time

时间:2016-10-06 11:11:44

标签: java mysql hibernate

我有一个对象,我使用 Hibernate逆向工程向导 Hibernate映射文件和数据库中的POJO 创建了它。

在这种情况下,我的表有一个sql时间字段( reminder_time ),但是hibernate将该字段生成为java.util.Date字段。

这是我的表,

enter image description here

上述字段为reminder_time。

这是我的目标,

import java.util.Date;


public class Reminder  implements java.io.Serializable {


     private Integer idreminder;
     private Patient patient;
     private String remindAbout;
     private Date reminderTime;
     private Date reminderDate;
     private boolean active;
     private boolean repeat;
     private Date dateCreated;
     private Date lastUpdated;

    public Reminder() {
    }


    public Reminder(Patient patient, String remindAbout, Date reminderTime, Date reminderDate, boolean active, boolean repeat, Date lastUpdated) {
        this.patient = patient;
        this.remindAbout = remindAbout;
        this.reminderTime = reminderTime;
        this.reminderDate = reminderDate;
        this.active = active;
        this.repeat = repeat;
        this.lastUpdated = lastUpdated;
    }
    public Reminder(Patient patient, String remindAbout, Date reminderTime, Date reminderDate, boolean active, boolean repeat, Date dateCreated, Date lastUpdated) {
       this.patient = patient;
       this.remindAbout = remindAbout;
       this.reminderTime = reminderTime;
       this.reminderDate = reminderDate;
       this.active = active;
       this.repeat = repeat;
       this.dateCreated = dateCreated;
       this.lastUpdated = lastUpdated;
    }

    public Integer getIdreminder() {
        return this.idreminder;
    }

    public void setIdreminder(Integer idreminder) {
        this.idreminder = idreminder;
    }
    public Patient getPatient() {
        return this.patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }
    public String getRemindAbout() {
        return this.remindAbout;
    }

    public void setRemindAbout(String remindAbout) {
        this.remindAbout = remindAbout;
    }
    public Date getReminderTime() {
        return this.reminderTime;
    }

    public void setReminderTime(Date reminderTime) {
        this.reminderTime = reminderTime;
    }
    public Date getReminderDate() {
        return this.reminderDate;
    }

    public void setReminderDate(Date reminderDate) {
        this.reminderDate = reminderDate;
    }
    public boolean isActive() {
        return this.active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
    public boolean isRepeat() {
        return this.repeat;
    }

    public void setRepeat(boolean repeat) {
        this.repeat = repeat;
    }
    public Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Date getLastUpdated() {
        return this.lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
}

当我尝试使用上面的对象将一些数据保存到此表中时,我遇到了以下异常,

Oct 06, 2016 4:40:06 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'repeat, date_created, last_updated) values (3, 'Taking medicine', '15:30:18', '2' at line 1
org.hibernate.exception.SQLGrammarException: could not execute statement

对此有任何想法吗?

1 个答案:

答案 0 :(得分:2)

repeatreserved keyword in MySQL,需要通过反引号转义或使用其他列名称。