编程语言
首页 > 编程语言> > Hibernate 5 Java 8 LocalDateTime

Hibernate 5 Java 8 LocalDateTime

作者:互联网

经过大量的阅读和尝试来自网络的解决方案后,我决定寻求帮助,因为没有一个解决方案尝试过.

我有一个非常简单的类,它有一个LocalDateTime变量.

我创建了一个MySQL表,我想存储包含这个变量的对象.对于LocalDateTime变量,我尝试过DateTime和TimeStamp类型.

据我所知,Hibernate 5应该支持java.time.localdatetime.正如我所说,我已经尝试使用timestamp类型和日期,只要timestamp和datetime作为Mysql列类型.

总是一样的错误.
这是我正在开始的一个新项目,我想开始使用新的Java 8 DateTime.

在这里,我附上所有类和配置文件.

这是Fecha.java,我想要在我的Mysql表上映射的对象.

@Entity
@Table(name = "Fecha", catalog = "qtx590", uniqueConstraints = { @UniqueConstraint(columnNames = { "_id" }) })
public class Fecha implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "_id", nullable = false, unique = true)
private int _id;
@Column(name = "_idEmpresa", nullable = false)
private int _idEmpresa;
@Column(name = "_idTurno", nullable = false)
private int _idTurno;
@Column(name = "Momento", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime momento;

public Fecha() {

    this._id = 99999;
    this._idEmpresa = 99999;
    this._idTurno = 99999;
    this.momento = LocalDateTime.now();
}

public Fecha(int _id, int _idEmpresa, int idTurno, LocalDateTime momento) {

    this._id = _id;
    this._idEmpresa = _idEmpresa;
    this._idTurno = idTurno;
    this.momento = momento;
}

public String getMomentoString() {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return this.momento.format(format);
}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public int get_idEmpresa() {
    return _idEmpresa;
}

public void set_idEmpresa(int _idEmpresa) {
    this._idEmpresa = _idEmpresa;
}

public int get_idTurno() {
    return _idTurno;
}

public void set_idTurno(int idTurno) {
    this._idTurno = idTurno;
}

public LocalDateTime getmomento() {
    return momento;
}

public void setmomento(LocalDateTime momento) {
    this.momento = momento;
}

}

这是HibernateConnectorClass

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateConnector {

private static HibernateConnector me;
private Configuration cfg;
private SessionFactory sessionFactory;

private HibernateConnector() throws HibernateException {

    cfg = new Configuration();
    sessionFactory = cfg.configure().buildSessionFactory();
}

public static synchronized HibernateConnector getInstance() throws HibernateException {
    if (me == null) {
        me = new HibernateConnector();
    }

    return me;
}

public Session getSession() throws HibernateException {
    Session session = sessionFactory.openSession();
    if (!session.isConnected()) {
        this.reconnect();
    }
    return session;
}

private void reconnect() throws HibernateException {
    this.sessionFactory = cfg.buildSessionFactory();
}
}

这是测试它的测试类:

public class Prueba {



public static void main(String[] args) {
    // TODO Auto-generated method stub


    FechaDAO fechaDAO = new FechaDAO();

    Fecha f = new Fecha();

    System.out.println(f.getmomento());

    fechaDAO.insertar(f);

    System.out.println("FIN");

}


}

hibernate.cfg.xml工作正常(我知道它,因为我用其他实体/表测试它.

这是Fecha Object的特定内容

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 31-ago-2015 12:58:20 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Fecha" table="Fecha">
    <id name="_id" type="java.lang.Integer" access="field">
        <column name="_id" />
        <generator class="increment" />
    </id>
    <property name="_idEmpresa" type="java.lang.Integer" access="field">
        <column name="_idEmpresa" />
    </property>
    <property name="_idTurno" type="java.lang.Integer">
        <column name="_idTurno" />
    </property>
    <property name="momento" type="java.time.LocalDateTime">
        <column name="Momento" />
    </property>
</class>
</hibernate-mapping>

而这个错误:

2015-09-02T13:47:16.719
sep 02, 2015 1:47:16 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.0.Final}
sep 02, 2015 1:47:16 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
sep 02, 2015 1:47:16 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
sep 02, 2015 1:47:16 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
sep 02, 2015 1:47:17 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
sep 02, 2015 1:47:17 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://qtx590.li-bra.es:3306/qtx590]
sep 02, 2015 1:47:17 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=qtx590, password=****}
sep 02, 2015 1:47:17 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
sep 02, 2015 1:47:17 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
sep 02, 2015 1:47:17 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
sep 02, 2015 1:47:17 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Hibernate: select max(_id) from Fecha
Hibernate: insert into Fecha (_idEmpresa, _idTurno, Momento, _id) values (?, ?, ?, ?)
sep 02, 2015 1:47:18 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 22001
sep 02, 2015 1:47:18 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Data truncation: Incorrect datetime value: '’' for column 'Momento' at row 1
sep 02, 2015 1:47:18 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
sep 02, 2015 1:47:18 PM org.hibernate.internal.SessionImpl$5 mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [could not execute statement]
org.hibernate.exception.DataException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2823)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3323)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:447)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:333)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:464)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2890)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2266)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:230)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
    at FechaDAO.insertar(FechaDAO.java:26)
    at Prueba.main(Prueba.java:16)
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '’' for column 'Momento' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2983)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 19 more
Hibernate: insert into Fecha (_idEmpresa, _idTurno, Momento, _id) values (?, ?, ?, ?)
sep 02, 2015 1:47:18 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 22001
sep 02, 2015 1:47:18 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Data truncation: Incorrect datetime value: '’' for column 'Momento' at row 1
sep 02, 2015 1:47:18 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "main" org.hibernate.exception.DataException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2823)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3323)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:447)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:333)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
    at FechaDAO.insertar(FechaDAO.java:32)
    at Prueba.main(Prueba.java:16)
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '’' for column 'Momento' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2983)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 11 more

解决方法:

IIRC for Hibernate5映射java.time.*没有@Temporal注释是需要/接受的. Hibernate5有足够的信息来判断属性类型的映射.这在他们的文件中说明.

标签:java,mysql,datetime,jodatime,hibernate-5-x
来源: https://codeday.me/bug/20190702/1358183.html