0

this is my code

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

It's very simple. But I don't understand why during JUnit Test the method getSingleResult() raise a classcast exception...

  • Same Context (more context same class can be provide this ex)
  • Check SerialID

Why happen?!?!

Thanks

UPDATE This is the stacktrace

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)

The line :40 is Applicazione applicazione = (Applicazione) query.getSingleResult();

3
  • can you update the post with exact stacktrace? and what is the type of Query ? in org.hibernate.Query I dont see getSingleResult() method. Commented Jul 9, 2013 at 12:46
  • Hi.. It's javax.persistence.query Commented Jul 9, 2013 at 13:03
  • a stacktrace will help SO users give quick accurate answer! Commented Jul 9, 2013 at 13:06

1 Answer 1

1

Looks like you have imported wrong Applicazione in your class (Since you have class name as same, I am assuming they are in different packages).

Check the import statement for Applicazione and fix it.

EDIT:

I just observed that you are using "from " + Applicazione.class.getName() +" a where a.id = :id" string. So if your query is returning result, even the assignment should work (without Exception) Cause you can't have simple name specified for 2 different package classes with same name, and one of them should have fully qualified class name . Are you sure you have posted the right code?

Sign up to request clarification or add additional context in comments.

3 Comments

Just checked... There is one and only one.. mmm
Are you sure about the stacktrace? your stacktrace conveys that you are casting 2 different classes
I agree with you. But I have one project (Spring Based) with models, dao and services. The model refer a Oracle table that extends Persistable<Long>. Have the serialID (to ensure the uniqueness of the object). Or it's a really stupid thing or a bug. Yes, I've posted the right code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.