Java Hibernate 无法反序列化错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20821896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate could not deserialize error
提问by user1174838
I have this Oracle table:
我有这个 Oracle 表:
SQL> Name Null? Type
----------------------------------------- -------- ----------------------------
JOB_ID NOT NULL VARCHAR2(13)
TYPE NOT NULL NUMBER
COMPONENT_DESCRIPTION NOT NULL VARCHAR2(255)
COMPONENT_ID VARCHAR2(13)
STATUS NOT NULL NUMBER(1)
REASON VARCHAR2(255)
NOTES VARCHAR2(255)
SQL>
There is no defined primary key but JOB_ID, TYPE and COMPONENT_DESCRIPTION combined are unique. I cannot make any changes to the database structure and the code I'm working will only ever read from the DB, it will never write to it.
没有定义的主键,但 JOB_ID、TYPE 和 COMPONENT_DESCRIPTION 组合是唯一的。我无法对数据库结构进行任何更改,并且我正在使用的代码只会从数据库中读取,而永远不会写入它。
I have made this Hibernate map file:
我制作了这个 Hibernate 地图文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="ARCHIVE">
<class name="myclass.ArchiveJobHeaderComponents" table="JOB_HEADER_COMPONENTS">
<composite-id>
<key-property name="jobId" column="JOB_ID" type="java.lang.String" />
<key-property name="type" column="TYPE" type="java.lang.Number" />
<key-property name="componentDescription" column="COMPONENT_DESCRIPTION" type="java.lang.String" />
</composite-id>
<property name="componentId" column="COMPONENT_ID" type="java.lang.String" not-null="false" />
<property name="status" column="STATUS" type="java.lang.Number" />
<property name="reason" column="REASON" type="java.lang.String" not-null="false" />
<property name="notes" column="NOTES" type="java.lang.String" not-null="false" />
</class>
<query name="JobHeaderComponents.lookupJobHeaderComponents">
<![CDATA[from myclass.ArchiveJobHeaderComponents where
jobId = :jobId and
type = :type and
componentDescription = :componentDescription ]]>
</query>
<query name="JobHeaderComponents.listJobHeaderComponentsByComponentId">
<![CDATA[from myclass.ArchiveJobHeaderComponents where componentId = :id]]>
</query>
</hibernate-mapping>
This is the corresponding Java class file:
这是对应的Java类文件:
package myclass;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.io.Serializable;
import java.lang.Number;
import java.util.HashSet;
public class ArchiveJobHeaderComponents implements Serializable {
private String jobId;
private Number type;
private String componentDescription;
private String componentId;
private Number status;
private String reason;
private String notes;
public String getJobId() {
return jobId;
}
public void setJobId(String jobId) {
this.jobId = jobId;
}
public Number getType() {
return type;
}
public void setType(Number type) {
this.type = type;
}
public String getComponentDescription() {
return componentDescription;
}
public void setComponentDescription(String componentDescription) {
this.componentDescription = componentDescription;
}
public String getComponentId() {
return componentId;
}
public void setComponentId(String componentId) {
this.componentId = componentId;
}
public Number getStatus() {
return status;
}
public void setStatus(Number status) {
this.status = status;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public int hashCode() {
return new HashCodeBuilder().
append(getJobId()).
append(getType()).
append(getComponentDescription()).
append(getComponentId()).
append(getStatus()).
append(getReason()).
append(getNotes()).toHashCode();
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ArchiveJobHeaderComponents)) {
return false;
}
ArchiveJobHeaderComponents that = (ArchiveJobHeaderComponents) o;
return new EqualsBuilder().append(this.getJobId(), that.getJobId()).
append(this.getType(), that.getType()).
append(this.getComponentDescription(), that.getComponentDescription()).
append(this.getComponentId(), that.getComponentId()).
append(this.getStatus(), that.getStatus()).
append(this.getReason(), that.getReason()).
append(this.getNotes(), that.getNotes()).isEquals();
}
public String toString() {
return new ToStringBuilder(this).
append("jobId", getJobId()).
append("type", getType()).
append("componentDescription", getComponentDescription()).
append("componentId", getComponentId()).
append("status", getStatus()).
append("reason", getReason()).
append("notes", getNotes()).toString();
}
}
Whenever I get data back from a query, I get 'Could not deserialize' followed by an 'EOFException' error.
每当我从查询中取回数据时,我都会收到“无法反序列化”,然后是“EOFException”错误。
I've checked:
- There are no variables in the Java class of type serialize
- The Java class is implementing Serializable
我检查过:
- 序列化类型的 Java 类中没有变量
- Java 类正在实现 Serializable
I don't want to split the three columns (JOB_ID, TYPE and COMPONENT_DESCRIPTION) into a separate 'Id' class as I'm having conceptual problems with how the data is accessed. (I realize this is not recommended but is supported).
我不想将三列(JOB_ID、TYPE 和 COMPONENT_DESCRIPTION)拆分为单独的“Id”类,因为我在访问数据的方式方面遇到了概念上的问题。(我意识到这是不推荐的,但支持)。
Can anyone point out what I've done wrong with how I've implemented this?
谁能指出我在如何实现这一点上做错了什么?
Thanks
谢谢
EDIT:
I've changed the hbm.xml to not have a composite key, just an id on JOB_ID with no improvement.
I've added not-null="false" to the columns that can be empty, also no improvement.
编辑:
我已经将 hbm.xml 更改为没有复合键,只是 JOB_ID 上的一个 id 没有改进。
我已经将 not-null="false" 添加到可以为空的列中,也没有改进。
采纳答案by Muel
Actually, having a look over the code and the Hibernate mapping file, I believe that the problem is that you are trying to map columns TYPE
and STATUS
to a Number
. Number
is an abstract class, so cannot be instantiated directly.
事实上,在看看在代码和Hibernate映射文件,我认为,问题是,你试图映射列TYPE
,并STATUS
到 Number
。Number
是一个抽象类,所以不能直接实例化。
As both TYPE
and STATUS
are NOT NULL
, I'd use primitive Java types to store their values, eg:
由于这两个TYPE
和STATUS
是NOT NULL
的,我会使用原始的Java类型来存储他们的值,例如:
public class ArchiveJobHeaderComponents implements Serializable {
private String jobId;
private int type; // int should give you a large enough range - but change to long if required
private String componentDescription;
private String componentId;
private boolean status; // status appears to be a boolean (NUMBER(1))
private String reason;
private String notes;
// remainder omitted
}
Also, please remember to update the Hibernate mapping file to reflect the above!!
另外,请记得更新 Hibernate 映射文件以反映上述内容!!
回答by konikoff
In case anyone else is working on a legacy Hibernate (3.0) app, something else that causes this error is running the app with Java 8 and OJDBC 1.4. Upgrading to OJDBC 6 resolved it.
如果其他人正在开发旧的 Hibernate (3.0) 应用程序,导致此错误的其他原因是使用 Java 8 和 OJDBC 1.4 运行应用程序。升级到 OJDBC 6 解决了这个问题。