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

huangapple go评论81阅读模式
英文:

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

问题

我对Java非常陌生,需要一些帮助。我使用OpenXava创建了一个@MappedSuperclass "Attendees"。

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

然后,我使用两个进一步的类"Staff"和"Participants"对其进行了扩展。

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元素的代码:

Address:

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;

...
}

MedicalInformation:

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;

...
}  

除了Attendees类,我省略了所有的getter()和setter()方法。

我将应用程序作为Java应用程序运行,一切正常,直到我尝试输入并保存记录。当我点击“保存”时,我收到以下提示:

无法执行保存操作: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
...

除此之外,在应用程序中我没有提到的其他实体允许我保存新记录。但是,这些实体与您提供的代码没有关联。

请帮助。

英文:

I am very new to Java and am in need of some help. I have created a @MappedSuperclass "Attendees" using OpenXava.

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

I then extended it with two further classes "Staff" and "Participants".

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;


... 
}

and

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;

...
}

Below is the code for the two @Embedded elements:

Address:

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;

...
}

and

MedicalInformation:

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;

...
}  

Apart from the Attendees class, I have omitted all getters() and setters().

I run the application as a Java Application and it works up until the point that I attempt to input and save a record. when I click "save" I am prompted with:

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

upon closer inspection in the consol of OpenXava I find the following:

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$1.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

other Entities in the application that I have not mentioned here, allow me to save new records. these Entities are however not linked or related to the provided code.

Please Help.

答案1

得分: 1

错误很明显:

"SQLDataException: 数据异常:字符串数据,右侧截断; 表:参与者,列:OID"

问题出在表格“参与者”的OID列(由于某种原因,您尝试插入的字符串被截断),可能与Jamie在以下链接中提到的内容有关:

https://stackoverflow.com/a/53182256/13745944

英文:

The error is clear:

"SQLDataException: data exception: string data, right truncation; table: PARTICIPANTS column: OID"

the problem is in the OID column in the table Participants (the string you are trying to insert is truncated for some reason) and it could be related to what said by jamie:

https://stackoverflow.com/a/53182256/13745944

答案2

得分: 1

将:

@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 = 3) 修改为 @Column(length = 32)。如果已经存在数据表,你需要删除表格,然后 OpenXava 会重新生成它们;如果数据表已经存在数据,你只需要执行 ALTER TABLE 操作来修改 OID 列的长度。

英文:

Change:

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

By:

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

Note @Column(length = 32) instead of @Column(length = 3). Also you have to drop your tables in order OpenXava generates them again, of if they already have data just do a ALTER TABLE to change the column length for OID column.

huangapple
  • 本文由 发表于 2020年10月25日 23:09:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64525255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定