Hiibernate:java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Integer

时间:2021-08-09 19:38:41

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:

我正在尝试为hibernate编写一个简单的代码。我使用SQL Server 2012作为数据库。我在运行应用程序时遇到以下错误:

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

2015年9月5日下午7:40:59 org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl发布INFO:HHH000010:在批量发布时它仍然包含JDBC语句

This is my Employee entity class

这是我的Employee实体类

 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.

类Employee的ID为整数,名称为String。请帮我解决一下这个。我是新手,并且正在努力学习。先感谢您。

1 个解决方案

#1


1  

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

它解决了。我重新创建了.hbm.xml文件。也许它将int作为name的数据类型。

#1


1  

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

它解决了。我重新创建了.hbm.xml文件。也许它将int作为name的数据类型。