【问题标题】:could not deserialize hibernate无法反序列化休眠
【发布时间】:2021-05-24 10:26:29
【问题描述】:

我在休眠中的序列化有问题。任何帮助,将不胜感激。堆栈跟踪如下:

Servlet.service() 用于 servlet [dispatcherServlet] 在上下文中的路径 [] 抛出异常 [请求处理失败;嵌套异常是 org.springframework.orm.jpa.JpaSystemException:无法反序列化; 嵌套异常是 org.hibernate.type.SerializationException: could 不反序列化] 有根本原因

java.io.EOFException: null at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2872) ~[na:na] 在 java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3367) ~[na:na] 在 java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:937) ~[na:na]

自定义类的实现

@Entity
@Table(name = "XBTB_BILLINGCUSTOM")
public class Custom  implements Serializable {

    private static final long serialVersionUID = 3L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Number id;
    String customerbranch;
    String invoicenumber;
    String registernumber;
    String trnrefno;
    String term;
    String invoicetype;
    String batchno;
    Number paymentamount;
    Number chargeamount;
    String authorize;
    String invoice;
    String search;

    public Custom(Number id, String customerbranch, String invocenumber, String registernumber, String trnrefno, String term, String invoicetype, String batchno, Number paymentamount, Number chargeamount, String authorize, String invoice, String search) {
        this.id = id;
        this.customerbranch = customerbranch;
        this.invoicenumber = invocenumber;
        this.registernumber = registernumber;
        this.trnrefno = trnrefno;
        this.term = term;
        this.invoicetype = invoicetype;
        this.batchno = batchno;
        this.paymentamount = paymentamount;
        this.chargeamount = chargeamount;
        this.authorize = authorize;
        this.invoice = invoice;
        this.search = search;
    }



    public Number getId() {
        return id;
    }

    public void setId(Number id) {
        this.id = id;
    }

    public String getCustomerbranch() {
        return customerbranch;
    }

    public void setCustomerbranch(String customerbranch) {
        this.customerbranch = customerbranch;
    }

    public String getInvocenumber() {
        return invoicenumber;
    }

    public void setInvocenumber(String invocenumber) {
        this.invoicenumber = invocenumber;
    }

    public String getRegisternumber() {
        return registernumber;
    }

    public void setRegisternumber(String registernumber) {
        this.registernumber = registernumber;
    }

    public String getTrnrefno() {
        return trnrefno;
    }

    public void setTrnrefno(String trnrefno) {
        this.trnrefno = trnrefno;
    }

    public String getTerm() {
        return term;
    }

    public void setTerm(String term) {
        this.term = term;
    }

    public String getInvoicetype() {
        return invoicetype;
    }

    public void setInvoicetype(String invoicetype) {
        this.invoicetype = invoicetype;
    }

    public String getBatchno() {
        return batchno;
    }

    public void setBatchno(String batchno) {
        this.batchno = batchno;
    }

    public Number getPaymentamount() {
        return paymentamount;
    }

    public void setPaymentamount(Number paymentamount) {
        this.paymentamount = paymentamount;
    }

    public Number getChargeamount() {
        return chargeamount;
    }

    public void setChargeamount(Number chargeamount) {
        this.chargeamount = chargeamount;
    }

    public String getAuthorize() {
        return authorize;
    }

    public void setAuthorize(String authorize) {
        this.authorize = authorize;
    }

    public String getInvoice() {
        return invoice;
    }

    public void setInvoice(String invoice) {
        this.invoice = invoice;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public Custom() {

    }

    @Override
    public String toString() {
        var builder = new StringBuilder();
        builder.append("Custom{id=").append(id)
                .append(", customerbranch=").append(customerbranch)
                .append(", invoicenumber=").append(invoicenumber)
                .append(", registernumber=").append(registernumber)
                .append(", trnrefno=").append(trnrefno)
                .append(", term=").append(term)
                .append(", invoicetype=").append(invoicetype)
                .append(", batchno=").append(batchno)
                .append(", paymentamount=").append(paymentamount)
                .append(", chargeamount=").append(chargeamount)
                .append(", authorize=").append(authorize)
                .append(", invoice=").append(invoice)
                .append(", search=").append(search).append("}");

        return builder.toString();
    }
}

CustomRepository 的实现

@Repository
@Transactional
public interface CustomRepository extends JpaRepository<Custom, Number> {

}

在这里,我调用了 CustomRepository,它以某种方式抛出了序列化错误。我不知道为什么,因为 cityRepository 工作正常。

@RestController
@RequestMapping(value = "/tax-requests", name = "Provides Tax service API.")
public class TaxRestApi {
    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final String ERR_MSG_INVALID_BASIC_AUTHORIZATION = "Invalid basic authorization!";
    private static final String ERR_MSG_NEGATIVE_AMOUNT = "Amount cannot be negative value!";

    public TaxRestApi(CustomRepository customRepository) {
        this.customRepository = customRepository;
    }

    @Autowired
    CustomRepository customRepository;
    @Autowired
    CityRepository cityRepository;

    @GetMapping(value = "/taxlist")
    public ResponseEntity<RestResult> login(){
        Number num = 2;
         Long res1 = customRepository.count();
         System.out.print(res1+"\n");
        System.out.printf("'%S' %n", "Jack");
        List<City> coll = cityRepository.findAll();
        List<Custom> cust = customRepository.findAll();
             List<Custom> res2 = new ArrayList<>();
             Custom custom = new Custom(888,"101","212","101",
                 "212","101","212","101",212,
                 444,"1","2","2");
        res2.add(custom);
        return RestResponse.success(coll);
    }

}

感谢您的宝贵时间

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    这可能是几件事

    • 试图反序列化一个空流
    • 尝试序列化非持久对象(不由 JPA 会话管理)
    • java类型和sql类型的映射不正确
    • 不正确的表连接

    您可以启用 SQL 日志记录来检查实际正在运行的查询

    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    logging.level.org.springframework.transaction=DEBUG
    logging.level.org.hibernate=DEBUG
    logging.level.org.hibernate.SQL=debug
    logging.level.org.hibernate.type.descriptor.sql=trace
    

    在您提供的示例中,您正在创建一个“new Custom(..)”,但从未将该对象传递给“persist”,因此休眠会话不知道该实体,因此无法对其进行序列化。

    打开上面的 SQL 日志记录应该会告诉你 hibernate 正在做什么。

    【讨论】:

    • 谢谢,我会检查上面提到的东西
    • 我的问题是我将一些变量声明为 Number 字段而不是 int。当我将这些字段更改为 int 字段时,它起作用了。
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 2011-12-27
    相关资源
    最近更新 更多