2013-07-09 166 views
0

過程中,這是我的代碼ClassCastException異常的JUnit

Query query = getEntityManager().createQuery("from " + Applicazione.class.getName() +" a where a.id = :id"); 
    query.setParameter("id", id); 
    Applicazione applicazione = (Applicazione) query.getSingleResult(); 

這很簡單。但我不JUnit測試過程中,爲什麼理解方法getSingleResult()養classcast例外...

  • 相同的上下文(更多情況下相同的類可以提供這種EX)
  • 檢查SerialID

爲什麼會發生?!?!

感謝

UPDATE 這是堆棧跟蹤

java.lang.ClassCastException: 
it.xxxxx.intranet.core.model.Applicazione cannot be cast to 
it.xxxxx.intranet.core.model.Applicazione 

java.lang.ClassCastException: it.XXXX.intranet.core.model.Applicazione cannot be cast to it.XXXX.intranet.core.model.Applicazione 
    at it.xxxxx.intranet.core.dao.impl.ApplicazioneDaoImpl.findApplicazioneById(ApplicazioneDaoImpl.java:40) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at $Proxy16.findApplicazioneById(Unknown Source) 
    at it.xxxxx.intranet.core.service.impl.ApplicazioneServiceImpl.findApplicazioneById(ApplicazioneServiceImpl.java:25) 
    at it.xxxxx.intranet.core.test.GestioneApplicazioniTest.detailApplicazioneTest(GestioneApplicazioniTest.java:44) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

線:40 Applicazione applicazione = (Applicazione) query.getSingleResult();

+0

你可以用精確的堆棧跟蹤來更新帖子嗎? 「Query」的類型是什麼?在'org.hibernate.Query'中我看不到'getSingleResult()'方法。 – sanbhat

+0

嗨..這是javax.persistence.query –

+0

stacktrace將幫助SO用戶提供快速準確的答案! – sanbhat

回答

1

看起來你已經匯入在您的類錯誤Applicazione(既然你有類名同樣,我假設他們在不同的包裝中)。

檢查import聲明Applicazione並修復它。

編輯:

我只是看到你正在使用"from " + Applicazione.class.getName() +" a where a.id = :id"字符串。因此,如果您的查詢返回結果,則即使該賦值也可以工作(無例外) 原因您不能爲具有相同名稱的2個不同包類指定簡單名稱,並且其中一個包含完全限定類名。你確定你已經發布了正確的代碼嗎?

+0

剛剛檢查過...有一個也是唯一一個.. mmm –

+0

您確定關於stacktrace嗎?你的stacktrace傳達你正在投2個不同的課 – sanbhat

+0

我同意你的看法。但我有一個項目(基於Spring的)與模型,道和服務。該模型引用了延續Persistable 的Oracle表。擁有serialID(確保對象的唯一性)。或者這是一個非常愚蠢的事情或一個錯誤。是的,我發佈了正確的代碼。 –