日期时间值不正确Hibernate

时间:2016-02-15 21:15:12

标签: hibernate date datetime time

我是熟悉hibernate并且还在学习的新手,我在下面的代码中获得了堆栈跟踪:

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.*;

@Entity
public class User {

    @Id
    private int id;
    private String name;

    private ProtienData protienData = new ProtienData();

    private List<UserHistory> history = new ArrayList<UserHistory>();

    public void addHistory(UserHistory historyItem) {
    historyItem.setUser(this);
    history.add(historyItem);
    }
    public ProtienData getProtienData() {
    return protienData;
    }
    public List<UserHistory> getHistory() {
    return history;
    }

    public void setHistory(List<UserHistory> history) {
    this.history = history;
    }
    public void setProtienData(ProtienData protienData) {
    this.protienData = protienData;
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
}

}

我的proteinData类如下:

package com.simpleprogrammer;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;

public class UserHistory {
private int id;
private User user;

private Date entryTime;
private String entry;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}


public UserHistory(Date entryTime, String entry) {
    this.entry = entry;
    this.entryTime = entryTime;
}
public UserHistory() {
}

public Date getEntryTime() {
    return entryTime;
}
public void setEntryTime(Date entryTime) {
    this.entryTime = entryTime;
}
public String getEntry() {
    return entry;
}
public void setEntry(String entry) {
    this.entry = entry;
}

}

我的User.java文件是:     package com.simpleprogrammer;

<class name="com.simpleprogrammer.UserHistory" table = "USERHISTORY">
<id name="id" type="int">
    <column name="ID"  />
    <generator class="increment"/>
</id>
<many-to-one name="user" class="com.simpleprogrammer.User" not-null="true">
    <column name="USER_ID" />
</many-to-one>
<property name="entryTime" type="java.util.Date">
    <column name="ENTRYTIME" />
</property>
<property name="entry" type="java.lang.String">
    <column name="ENTRY" />
</property>
</class>

我的UserHistory类如下:

    <component name = "protienData">
        <property name="total" type="int">
            <column name="TOTAL" />
        </property>
        <property name="goal" type="int">
            <column name="GOAL" />
        </property>
    </component>
    <list name = "history" table="USER_HISTORY" inverse="true" cascade="save-update">
        <key column="USER_ID"/>
        <list-index column="POSITION"/>
        <one-to-many class="com.simpleprogrammer.UserHistory"/>
    </list>
</class>
</hibernate-mapping>

}

我的一个hibernate映射文件如下:(删除了标题等)

Exception in thread "main" org.hibernate.exception.DataException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:71)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3067)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at com.simpleprogrammer.Program.main(Program.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'ENTRYTIME' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2868)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
    at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
    at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    ... 19 more

另一个如下:                                                                                     

{{1}}

我尝试运行时遇到以下错误:

{{1}}

有人知道造成这种情况的原因吗?

3 个答案:

答案 0 :(得分:1)

原来,它是我的SQL Connector J Jar文件的过时版本。

答案 1 :(得分:0)

根据您的休眠配置,您可能会在使用session.getTransaction.comit()后关闭会话;

答案 2 :(得分:0)

您的错误是:

Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'ENTRYTIME' at row 1

您如何定义&#39; ENTRYTIME&#39; USER_HISTORY数据库表上的列?