could not deserialize hibernate

Total
1
Shares

I have a problem with the serialization in hibernate. Any help would be appreciated. The stack trace is the Following:

Servlet.service() for servlet [dispatcherServlet] in context with path
[] threw exception [Request processing failed; nested exception is
org.springframework.orm.jpa.JpaSystemException: could not deserialize;
nested exception is org.hibernate.type.SerializationException: could
not deserialize] with root cause

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

Implementation of the Custom class

@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();
    }
}

Implementation of the CustomRepository

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

}

Here I call the CustomRepository and somehow it is throwing the serialization error. I don’t know why because the cityRepository is working fine.

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

}

Thank you for your time


Solution

It could be several things

  • Attempting to deserialize an empty stream
  • Attempting to serialize a non-persisted object (not managed by a JPA session)
  • Improper mapping between the java type, and the sql type
  • Improper table join

You can enable the SQL logging to inspect what queries are actually being run

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

In the sample you provided you are creating a ‘new Custom(..)’ but never passing that object to ‘persist’ so the hibernate session is not aware of the entity and therefore can not serialize it.

Turning on the SQL logging above should tell you everything that hibernate is doing.

Leave a Reply

Your email address will not be published. Required fields are marked *