繁体   English   中英

不正确的日期时间值休眠

[英]Incorrect datetime value Hibernate

我是刚进入休眠状态并仍在学习的人,我正在获取以下代码的堆栈跟踪:

package com.simpleprogrammer;

import org.hibernate.Session;
import java.util.Date;

public class Program {
public static void main(String[] args) {
    System.out.println(org.hibernate.Version.getVersionString());
    System.out.println("Creating session!");
    Session session = HibernateUtilities.getSessionFactory().openSession();
    session.beginTransaction();

    User user = new User();
    user.setName("Joe");
    user.addHistory(new UserHistory(new Date(), "Set the name to Joe"));
    System.out.println("Name set to Joe!!");
    user.getProtienData().setGoal(250);
    user.addHistory(new UserHistory(new Date(), "Set the goal to 250"));
    System.out.println("Goal set to 250!!");
    session.save(user);
    System.out.println("Session saved");

    session.getTransaction().commit();
    System.out.println("Commit done - well done!!");
    System.out.println("Beginning transaction!");

    session.beginTransaction();

    User loadedUser = (User) session.load(User.class, 1);
    System.out.println("Name of user is " + loadedUser.getName());
    System.out.println("Number of goal is " + loadedUser.getProtienData().getGoal());
    for(UserHistory history : loadedUser.getHistory())
    {
        System.out.println(history.getEntryTime().toString() + " " + history.getEntry());
    }

    loadedUser.getProtienData().setTotal(loadedUser.getProtienData().getTotal() + 50);
    loadedUser.addHistory(new UserHistory(new Date(), "Added 50 protein"));
    System.out.println("Total of user is " + loadedUser.getProtienData().getTotal());

    session.getTransaction().commit();
    System.out.println("After commit, total of user is " + loadedUser.getProtienData().getTotal());
    session.close();
    HibernateUtilities.getSessionFactory().close();
}

}

我的proteinData类如下:

package com.simpleprogrammer;
public class ProtienData {

private int total;
private int goal;

public int getGoal() {
    return goal;
}
public void setGoal(int goal) {
    this.goal = goal;
}
public int getTotal() {
    return total;
}
public void setTotal(int total) {
    this.total = total;
}

}

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

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;
    }
}

我的UserHistory类如下:

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;
}

}

我的休眠映射文件之一如下:(删除了标题等)

<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>

另一个如下:

    <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>

尝试运行此命令时出现以下错误:

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

有人知道是什么原因造成的>

原来,这是我的SQL Connector J Jar文件的最新版本。

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

嗨,您遇到的错误是:

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

您如何在USER_HISTORY数据库表上定义“ ENTRYTIME”列?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM