Hibernate / jpa将Java日期持久化到H2 DB Sql TimeStamp

时间:2014-07-16 11:40:20

标签: java sql hibernate persistence

我试图将实体创建日期作为时间戳保存到数据库(h2)中。 这是代码:

@Temporal(TemporalType.TIMESTAMP)
@Required
public Date creationDate = new.Date();

我得到的错误:

[PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query] 
Caused by: org.h2.jdbc.JdbcSQLException: Blad skladniowy w wyrazeniu SQL "INSERT INTO MedicalIncident (customer_id, actionTakenAfterIncident, actionTypeTakenAfterIncident, year_id, age_id, incidentOccurencePatientInformation, month_id, sex_id, patientHarm, patientHarmType_id, patientHarmType_Other, unit_id, place_id, incidentType_id, incidentType_Diagnosis_id, incidentType_Infection_id, incidentType_MedicineApplication_id, incidentType_PatientRightsViolation_id, incidentType_Surgery_id, incidentType_Therapy_id, incidentType_WorkOrganization_id, incidenttype_other, creationDate ) VALUES (1,true,'Różniejsze',2014,37,false,02,1,true,4,'',12,5,3,null,null,null,null,2,null,null,','Wed Jul 16 13? CEST 2014[*]')"
    Syntax error in SQL statement "INSERT INTO MedicalIncident (customer_id, actionTakenAfterIncident, actionTypeTakenAfterIncident, year_id, age_id, incidentOccurencePatientInformation, month_id, sex_id, patientHarm, patientHarmType_id, patientHarmType_Other, unit_id, place_id, incidentType_id, incidentType_Diagnosis_id, incidentType_Infection_id, incidentType_MedicineApplication_id, incidentType_PatientRightsViolation_id, incidentType_Surgery_id, incidentType_Therapy_id, incidentType_WorkOrganization_id, incidenttype_other, creationDate ) VALUES (1,true,'Różniejsze',2014,37,false,02,1,true,4,'',12,5,3,null,null,null,null,2,null,null,','Wed Jul 16 13? CEST 2014[*]')"; SQL statement:
    INSERT INTO MedicalIncident (customer_id, actionTakenAfterIncident, actionTypeTakenAfterIncident, year_id, age_id, incidentOccurencePatientInformation, month_id, sex_id, patientHarm, patientHarmType_id, patientHarmType_Other, unit_id, place_id, incidentType_id, incidentType_Diagnosis_id, incidentType_Infection_id, incidentType_MedicineApplication_id, incidentType_PatientRightsViolation_id, incidentType_Surgery_id, incidentType_Therapy_id, incidentType_WorkOrganization_id, incidenttype_other, creationDate ) VALUES (1,true,'Różniejsze',2014,37,false,02,1,true,4,'',12,5,3,null,null,null,null,2,null,null,','Wed Jul 16 13? CEST 2014') [42000-172]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:169) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:146) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.getSyntaxError(DbException.java:181) ~[h2.jar:1.3.172]
        at org.h2.command.Parser.getSyntaxError(Parser.java:484) ~[h2.jar:1.3.172]
        at org.h2.command.Parser.checkRunOver(Parser.java:3318) ~[h2.jar:1.3.172]
        at org.h2.command.Parser.initialize(Parser.java:3234) ~[h2.jar:1.3.172]
        at org.h2.command.Parser.parse(Parser.java:266) ~[h2.jar:1.3.172]
        at org.h2.command.Parser.parse(Parser.java:255) ~[h2.jar:1.3.172]
        at org.h2.command.Parser.prepareCommand(Parser.java:218) ~[h2.jar:1.3.172]
        at org.h2.engine.Session.prepareLocal(Session.java:425) ~[h2.jar:1.3.172]
        at org.h2.engine.Session.prepareCommand(Session.java:374) ~[h2.jar:1.3.172]
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138) ~[h2.jar:1.3.172]
        at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:70) ~[h2.jar:1.3.172]
        at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:267) ~[h2.jar:1.3.172]
        at com.jolbox.bonecp.ConnectionHandle.prepareStatement(ConnectionHandle.java:1024) ~[bonecp.jar:na]
        at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:202) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        ... 30 common frames omitted

我认为这是由不正确的日期格式引起的。 日期是这样的: '7月16日星期三13? CEST 2014' 时间戳是一个数字f.e。: 124144324

我还创建了明确的类来检查暂时持久性。 这是代码

    package models;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import play.db.jpa.JPA;
import play.db.jpa.Transactional;

@Entity
public class Uss {

    @Id
    public Long id; 
    @Column(name="name")  
    public  String name;
    @Temporal(TemporalType.TIMESTAMP)
    public Date createDate = new Date();


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

 public String toString() {
     return "uss class: name=" + this.name + ", id=" + this.id + ", createDate=" + this.createDate; 
 }
 public int toDatabase() {
     JPA.em().persist(this);
    return 1;

 }

@Transactional
public static Uss getById(Long id) {
    return JPA.em().find(Uss.class, id);
}

}

这个很棒。在数据库中,我有时间戳列,条目如下: 2014-07-17 10:24:14.357 当我得到这个实体时,我可以使用java.util.date对象的时间戳(因为它是java.util.date对象)

在我的第一个例子中(一个dosnt工作)我不使用persist方法。 我正在使用“原生查询”

String sqlQueryString = "Insert into table () values ();
Query tempQuery = JPA.em().createNativeQuery(sqlQueryString);
    tempQuery.executeUpdate();

1 个答案:

答案 0 :(得分:1)

我终于成功了。

变量声明:

@Temporal(TemporalType.TIMESTAMP)
public Date createDate;

插入方法:

String sqlQuery = "INSERT INTO Incident "
            + "(customer_id, unit_id, place_id, "
            + "incidentType_id, createDate ) "
            + "VALUES ("+ this.customer.id +","+ this.unit.id +","+ this.place.id +","
            + this.incidentType.id +",'"+ Index.getDate("yyyy-MM-dd HH:mm:ss") + "')" ;
Query tempQuery = JPA.em().createNativeQuery(sqlQuery);
tempQuery.executeUpdate();

或者只是坚持模型类:

     JPA.em().persist(this);

在数据库中,我最终输入的日期为sql timestamp类型,例如:

CREATEDATE  
2014-07-17 15:56:28.0