Hibernate无法反序列化错误

 手机用户2502896697 发布于 2023-02-07 17:17

我有这个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>

没有定义的主键,但组合的JOB_ID,TYPE和COMPONENT_DESCRIPTION是唯一的.我不能对数据库结构进行任何更改,而我正在工作的代码只会从DB中读取,它永远不会写入它.

我制作了这个Hibernate地图文件:






    
        
        
        
    
    
    
    
    



    



    



这是相应的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();
    }

}

每当我从查询中获取数据时,我都会得到"无法反序列化",然后出现"EOFException"错误.

我检查过:
- serialize类型的Java类中没有变量
- Java类正在实现Serializable

我不想将三列(JOB_ID,TYPE和COMPONENT_DESCRIPTION)拆分成一个单独的'Id'类,因为我对如何访问数据存在概念上的问题.(我意识到这不是推荐但是支持).

任何人都可以指出我做错的方法是什么?

谢谢

编辑:
我已经将hbm.xml更改为没有复合键,只是JOB_ID上的id而没有任何改进.
我已经将not-null ="false"添加到可以为空的列中,也没有任何改进.

1 个回答
  • 实际上,看看代码和Hibernate映射文件,我相信问题是你正在尝试映射列TYPESTATUS一个 Number.Number是一个抽象类,因此无法直接实例化.

    由于这两个TYPESTATUSNOT 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
    }
    

    另外,请记得更新Hibernate映射文件以反映上述内容!

    2023-02-07 17:18 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有