2011-12-04 6 views
0

は、私は次のエラーに「メイン」とjava.lang.ClassCastExceptionスレッドで春 - 初級レベル

例外を取得しています:org.hibernate.impl.SessionFactoryImplは in.codejavaにキャストすることはできません。

ApplicationContext context= new ClassPathXmlApplicationContext("Spring-Module.xml"); 
HibCustomer CustomerDao = (HibCustomer) context.getBean("sessionFactory"); 
: in.codejava.spring.withHibernate.App.main(App.java:15)

これは私のコードから行でのspring.withHibernate.customer.dao.impl.HibCustomer

何が原因の可能性がありますか?

+0

あなたのセッションファクトリは、HibernateがSessionFactoryImpl' 'に設定されています。 有効なセッションファクトリの実装であれば、intを 'HibCustomer'に設定しようとします。 – Yappie

答えて

0

一般に、「sessionFactory」という名前のオブジェクトはHibCustomerのインスタンスではない可能性が非常に高いです。あなたの場合、それは明らかです、ちょうどスタックトレースを見てください。これは、あなたに、org.hibernate.customer.dao.impl.HibCustomer内のorg.hibernate.impl.SessionFactoryImplのインスタンスをキャストできないことを伝えます。

は、ほとんどの場合、あなたはそこからのSessionFactoryにキャストして続行します:

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