简体   繁体   中英

Incorrect datetime value Hibernate

I am new to hibernate and still learning, I'm getting the stack trace below for the following code:

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

}

My proteinData class is as follows:

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

}

And my User.java file is: 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;
    }
}

My UserHistory class is as follows:

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

}

One of my hibernate mapping file is as follows: (with the header etc removed)

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

and the other one is as follows:

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

I'm getting the following error when I try to run this:

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

Does anybody know what is causing this>

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

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

Hi the error that you have is:

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

How do you define 'ENTRYTIME' Column on your USER_HISTORY database table?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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