"Impossible to execute Save action: org.hibernate.exception.DataException: could not execute statement" 使用 OpenXava?

"Impossible to execute Save action: org.hibernate.exception.DataException: could not execute statement" using OpenXava?

我是 Java 的新手,需要一些帮助。我使用 OpenXava 创建了一个@MappedSuperclass“与会者”。

import javax.persistence.*;
import org.hibernate.annotations.*;
import org.openxava.annotations.*;

@MappedSuperclass
public class Attendees {
    
    @Id @Hidden @Column(length = 3)
    @GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy="uuid")
    private String oid;

    public String getOid() {
        return oid;
    }
    public void setOid(String oid) {
        this.oid = oid;
    }
}

然后我又扩展了两个 classes“员工”和“参与者”。

import javax.persistence.*;

@Entity
public class Staff extends Attendees{
    
    @Column (length = 50) 
    private String fullName;
    
    @Column (length = 2) 
    private int age;

    @Column (length = 10) 
    private int contactNum;
    
    @Embedded 
    private Address address; 

    @Embedded
    private MedicalInformation medicalInfo;


... 
}

import javax.persistence.*;

@Entity
public class Participants extends Attendees{
    
    @Column (length = 50) 
    private String fullName;
    
    @Column (length = 2) 
    private int age;

    @Column (length = 10) 
    private int contactNum;
    
    @Embedded 
    private Address address; 

    @Embedded
    private MedicalInformation medicalInfo;

...
}

下面是两个@Embedded 元素的代码:

地址:

import javax.persistence.*;

@Embeddable
public class Address {
    
    @Column(length = 100)
    private String street;
    
    @Column(length = 100)
    private String suburb;
 
    @Column(length = 100)
    private String city;
 
    @Column(length = 100)
    private String province;
    
    @Column(length = 5)
    private int areaCode;

...
}

医疗信息:

import javax.persistence.*;

@Embeddable
public class MedicalInformation {
    
    @Column (length = 3)
    private String dailyMedication;
    
    @Column (length = 300)
    private String medicationDescription;
    
    @Column (length = 300)
    private String allergies;
    
    @Column (length = 300)
    private String dietRequirements;
    
    @Column (length = 300)
    private String additionalNotes;

...
}  

除了与会者 class,我省略了所有 getters() 和 setters()。

我将应用程序 运行 作为一个 Java 应用程序,它会一直运行到我尝试输入和保存记录为止。当我点击“保存”时,系统提示我:

 Impossible to execute Save action: org.hibernate.exception.DataException: could not execute statement

在 OpenXava 的控制台中仔细检查后,我发现以下内容:

Oct 25, 2020 5:03:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 3401, SQLState: 22001
Oct 25, 2020 5:03:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: data exception: string data, right truncation;  table: PARTICIPANTS column: OID
Oct 25, 2020 5:03:36 PM org.openxava.controller.ModuleManager manageRegularException
SEVERE: org.hibernate.exception.DataException: could not execute statement
javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1459)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1439)
    at org.openxava.jpa.impl.EntityManagerDecorator.flush(Unknown Source)
    at org.openxava.model.impl.JPAPersistenceProvider.flush(Unknown Source)
    at org.openxava.model.impl.MapFacadeBean.commitTransaction(Unknown Source)
    at org.openxava.model.impl.MapFacadeBean.create(Unknown Source)
    at org.openxava.model.MapFacade.create(Unknown Source)
    at org.openxava.actions.SaveAction.create(Unknown Source)
    at org.openxava.actions.SaveAction.execute(Unknown Source)
    at org.openxava.controller.ModuleManager.executeAction(Unknown Source)
    at org.openxava.controller.ModuleManager.executeAction(Unknown Source)
    at org.openxava.controller.ModuleManager.execute(Unknown Source)
    at org.apache.jsp.xava.execute_jsp._jspService(execute_jsp.java:266)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:580)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:516)
    at org.openxava.web.servlets.Servlets.getURIAsStream(Unknown Source)
    at org.openxava.web.dwr.Module.getURIAsStream(Unknown Source)
    at org.openxava.web.dwr.Module.request(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at org.directwebremoting.impl.ExecuteAjaxFilter.doFilter(ExecuteAjaxFilter.java:34)
    at org.directwebremoting.impl.DefaultRemoter.doFilter(DefaultRemoter.java:428)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:431)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:283)
    at org.directwebremoting.servlet.PlainCallHandler.handle(PlainCallHandler.java:52)
    at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
    at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: 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:113)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3174)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3688)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1453)
    ... 60 more
Caused by: java.sql.SQLDataException: data exception: string data, right truncation;  table: PARTICIPANTS column: OID
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:136)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:136)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
    ... 68 more

我在这里没有提到的应用程序中的其他实体,允许我保存新记录。然而,这些实体与提供的代码没有链接或相关。

请帮忙。

错误很明显:

"SQLDataException: 数据异常:字符串数据,右截断;table: PARTICIPANTS 列:OID"

问题出在 table 参与者的 OID 列中(您尝试插入的字符串由于某种原因被截断了),这可能与 jamie 所说的有关:

变化:

@Id @Hidden @Column(length = 3)
@GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy="uuid")
private String oid;

作者:

@Id @Hidden @Column(length = 32)
@GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy="uuid")
private String oid;

注意@Column(length = 32) 而不是@Column(length = 3)。此外,您还必须删除表,以便 OpenXava 再次生成它们,如果它们已经有数据,只需执行 ALTER TABLE 来更改 OID 列的列长度。