Spring与Hibernate错误-初学者级别

Spring与Hibernate错误-初学者级别,hibernate,spring,Hibernate,Spring,我得到以下错误 线程“main”java.lang.ClassCastException中的异常:org.hibernate.impl.SessionFactoryImpl无法强制转换为 in.code java.spring.withHibernate.customer.dao.impl.HibCustomer at in.codejava.spring.withHibernate.App.main(App.java:15) 这是我的代码中的一行: ApplicationContext con

我得到以下错误

线程“main”java.lang.ClassCastException中的异常:org.hibernate.impl.SessionFactoryImpl无法强制转换为 in.code java.spring.withHibernate.customer.dao.impl.HibCustomer at in.codejava.spring.withHibernate.App.main(App.java:15)

这是我的代码中的一行:

ApplicationContext context= new ClassPathXmlApplicationContext("Spring-Module.xml");
HibCustomer CustomerDao = (HibCustomer) context.getBean("sessionFactory");

可能是什么错误?

通常,名为“sessionFactory”的对象很可能不是HibCustomer的实例。在您的情况下,很明显,只需查看堆栈跟踪。它告诉您不能将org.hibernate.impl.SessionFactoryImpl实例强制转换为in.codejava.spring.withHibernate.customer.dao.impl.hibcuster

您很可能希望将其强制转换到SessionFactory并从那里继续:

import org.hibernate.SessionFactory;
...
SessionFactory CustomerDao = (SessionFactory) context.getBean("sessionFactory");

您的会话工厂由Hibernate设置为
SessionFactoryImpl
。如果是有效的会话工厂实现,请尝试将int设置为
HibCustomer