JPA异常“内部异常:com.mysql.jdbc.MysqlDataTruncation:数据截断:日期时间值不正确”

时间:2016-08-02 11:28:38

标签: java jpa

我正在尝试使用Java Persistence API将值插入MySQL数据库,但我的代码以异常退出。完整输出:

    02-Aug-2016 14:13:29.946 SEVERE [http-nio-8084-exec-167] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [PostSessionServlet] in context with path [/edevize-tomcat-server] threw exception
 javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'lastActive' at row 1
Error Code: 0
Call: INSERT INTO session (endTime, idPlanTarifar, lastActive, sessionNumber, startTime, userid) VALUES (?, ?, ?, ?, ?, ?)
    bind => [6 parameters bound]
Query: InsertObjectQuery(edevize.jpa.entities.Session[ idSesiune=null ])
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at edevize.tomcat.server.PostSessionServlet.doPost(PostSessionServlet.java:64)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException

课程会话中的片段:

@Column(name = "lastActive")
@Temporal(TemporalType.TIMESTAMP)
private Date lastActive;
...
public Date getLastActive() {
    return lastActive;
}

public void setLastActive(Date lastActive) {
    this.lastActive = lastActive;
}

post方法的片段:

Calendar now = Calendar.getInstance();
...
Session currentSession = new Session();

currentSession.setLastActive(now.getTime());
...
em.persist(currentSession);

em.getTransaction().commit();

类Session中的变量lastActive属于java.util.Date类型,我使用带参数Calendar.getInstance().getTime()的setter设置它。

我尝试删除我设置变量lastActive(和另一个相同类型的变量)的行,代码正常工作,按预期将空值写入数据库。

我该如何解决这个问题?

编辑:我所有与时间相关的列都是TIMESTAMP

类型

0 个答案:

没有答案