0

I've done a program where I have to retrieve the data from a table in MS SQL. I'm using Hibernate where AbtDebtbyCAN is an Entity class. The connection so far is fine but the only problem I'm facing is printing out data from MS SQL using Annotation Mapping. Debt is the name of the table(the name of the table is in lower case) which is to be mapped with. Below here is the result I want to print it out using Hibernate. Can anybody help me on how to achieve the fetching of data?

debt

  id          can           bdrl_debt    excess_ta_debt      posting_ref   debt_settlement_id   debt_settlement_at       debt_business_date
  
11425   1099112400000003        0              200             501728             137        2020-10-13 10:51:50.000         2020-10-13 

AbtDebtbyCAN

@Data
@Entity
@Table(name = "debt")
public class AbtDebtbbyCAN implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int debt_id;

@Column(name = "can")
private int debt_can;

@Column(name = "bdrl_debt")
private int bdrl_debt;

@Column(name = "excess_ta_debt")
private int excess_ta_debt;

@Column(name = "posting_ref")
private int posting_ref;

@Column(name = "debt_settlement_id")
private int debt_settlement_id;

@Column(name = "debt_settlement_at")
private DateTime debt_settlement_at;

@Column(name = "debt_business_date")
private Date debt_business_date;

}

AbtCANMapperTest

public class AbtCANMapperTest {
    public static void main(String args[]) {

    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();

    SessionFactory factory = meta.getSessionFactoryBuilder().build();




        Session session = factory.openSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            List abtdebt = session.createQuery("FROM AbtDebtbbyCAN WHERE debt_can=1099112400000003").getResultList();
            for (Iterator iterator = abtdebt.iterator(); iterator.hasNext(); ) {
                AbtDebtbbyCAN abtcan = (AbtDebtbbyCAN) iterator.next();
                System.out.print("debt id: " + abtcan.getDebt_id());
                System.out.print("debt can: " + abtcan.getDebt_can());
                System.out.print("bdrl debt: " + abtcan.getBdrl_debt());
                System.out.print("excess debt: " + abtcan.getExcess_ta_debt());
                System.out.print("posting ref: " + abtcan.getPosting_ref());
                System.out.print("debt settlement id: " + abtcan.getDebt_settlement_id());
                System.out.print("debt settlement at: " + abtcan.getDebt_settlement_at());
                System.out.println("debt business date: " + abtcan.getDebt_business_date());
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }


}
}

Error StackTrace

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    at org.hibernate.query.Query.getResultList(Query.java:165)
    at AbtMainTestControl.AbtTestObj.AbtCANMapperTest.main(AbtCANMapperTest.java:36)
Caused by: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
    at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:29)
    at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:60)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3088)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1907)
    at org.hibernate.loader.Loader.hydrateEntityState(Loader.java:1835)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1808)
    at org.hibernate.loader.Loader.getRow(Loader.java:1660)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:745)
    at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1044)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:995)
    at org.hibernate.loader.Loader.doQuery(Loader.java:964)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:350)
    at org.hibernate.loader.Loader.doList(Loader.java:2887)
    at org.hibernate.loader.Loader.doList(Loader.java:2869)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2701)
    at org.hibernate.loader.Loader.list(Loader.java:2696)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1415)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    ... 2 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 0000AC53
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:899)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:357)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:309)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:299)
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:218)
    ... 32 more
Caused by: org.hibernate.type.SerializationException: could not deserialize


> Task :AbtCANMapperTest.main() FAILED

Caused by: java.io.StreamCorruptedException: invalid stream header: 0000AC53

Execution failed for task ':AbtCANMapperTest.main()'.
> Process 'command 'C:/Program Files/Java/jdk1.8.0_241/bin/java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
5
  • Can anybody tell me what's the issue? I'm desperate for an answer Commented Nov 2, 2020 at 9:47
  • Will a can value of 16 digits actually deserialize into a Java int? Maybe you need a long instead? Commented Nov 2, 2020 at 10:23
  • @AlwaysLearning I'll try that out first. It seems that Hibernate mapping only works if all of the variables have the complementary data types to that of the objects in MS SQL table? Am I right along this line? Commented Nov 2, 2020 at 14:17
  • @AlwaysLearning I still get the same error. How do I solve it? Commented Nov 3, 2020 at 1:34
  • I've managed to solve it. I used String for all of my variables and I managed to retrieve it. Commented Nov 3, 2020 at 9:39

1 Answer 1

1

Not sure what types you are exactly using here, but at least DateTime is not a standard type that is supported by Hibernate. If Hibernate does not know about a type, it tries to flush the value in its serialized form as blob/byte[]. If you would use java.sql.Timestamp, java.util.Calendar or any other type that is supported by Hibernate out of the box, this should work properly.

Sign up to request clarification or add additional context in comments.

1 Comment

Initially, I chose DateTime because the data type of my variables have to be matched with the data type of the object in MS SQL. But, upon realising the fact that it could not deserialise, I changed it to String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.