我正在尝试为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)
2015年9月5日下午7:40:59 org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release 信息:HHH000010:批量发布时,它仍然包含JDBC语句
这是我的员工实体类
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;
}
}
这是我与数据库进行通信的代码。
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();
}
}
类Employee的ID为整数,名称为String。请帮我解决一下这个。我是新手,并且正在努力学习。提前谢谢。
答案 0 :(得分:1)
它解决了。我重新创建了.hbm.xml文件。也许它将int作为name的数据类型。