1

I am trying to write a simple code for hibernate. I am using SQL Server 2012 as a database. I am encountering the following error while running the application:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:36)
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:64)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:282)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3587)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:103)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at com.samples.test.HibernateUtil.main(HibernateUtil.java:28)

Sep 05, 2015 7:40:59 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release INFO: HHH000010: On release of batch it still contained JDBC statements

This is my Employee entity class

 package com.samples.hibernate;

public class Employee {

    private int ID;
    private String name;

    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Here is my code where I am communicating with the database.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    Session session = null;
    try
    {
        session = factory.openSession();
        Employee employee = new Employee();
        employee.setID(3);
        employee.setName("Venkatesh");

        session.beginTransaction();
        session.save(employee);
        session.getTransaction().commit();


    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        session.close();
    }
}

Class Employee has ID as integer and name as String. Please help me with this. I am new to hibernate and trying to learn. Thank You in advance.

4
  • 1
    Can you post your Employee entity class? Commented Sep 6, 2015 at 0:12
  • @dazito I have now included my Employee class. Thank You Commented Sep 6, 2015 at 1:11
  • 1
    Because you used XML configurations, can you add that too? My guess is that your DB originally had String values into the variable name. Try adding the hbm2ddl property of "Create" -> "<property name="hbm2ddl.auto">create-drop</property>" and refresh your table to see whether the problem persists. Commented Sep 6, 2015 at 1:22
  • The table was created using hbm2ddl update property Commented Sep 6, 2015 at 2:59

1 Answer 1

1

It is solved. I recreated .hbm.xml file. Maybe it was taking int as datatype for name.

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

Comments

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.