Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
1  
Can you post your Employee entity class? – dazito Sep 6 '15 at 0:12
    
@dazito I have now included my Employee class. Thank You – Venky Sep 6 '15 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. – OneRaynyDay Sep 6 '15 at 1:22
    
The table was created using hbm2ddl update property – Venky Sep 6 '15 at 2:59

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.