Announcement Announcement Module
Collapse
No announcement yet.
Spring + Hibernate: java.lang.ClassCastException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + Hibernate: java.lang.ClassCastException

    Hello, I am very new to Spring + Hibernate, and I am running tests to see if my DAOs are working. I get the following error which upon searching in Google yields a lot of different answers. I'm hoping you can help me out on this:

    Code:
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    	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:70)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nominationDao' defined in file [C:\IdeaProjects\Projects\branch\dev\test-dataacess\target\classes\com\dev\test\data\dao\hibernate\NominationHibernateDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.dev.test.data.dao.hibernate.NominationHibernateDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    	... 27 more
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.dev.test.data.dao.hibernate.NominationHibernateDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
    	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    	... 42 more
    Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    	at com.dev.test.data.dao.hibernate.AbstractHibernateDAO.<init>(AbstractHibernateDAO.java:50)
    	at com.dev.test.data.dao.hibernate.NominationHibernateDAO.<init>(NominationHibernateDAO.java:36)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    	... 44 more
    What could cause this? Please let me know if I have to post AbstractHibernateDAO and NominationHibernateDAO (the post has a 10k character limit).

    Thank you!
    Last edited by nimchip; Jan 18th, 2012, 03:25 PM.

  • #2
    This is a snippet for AbstractHibernateDAO (line 50 is bolded):

    Code:
    public abstract class AbstractHibernateDAO<T, K extends Serializable>
            implements DAO<T, K> {
       
        protected Logger logger = LoggerFactory.getLogger(this.getClass());
       
        private SessionFactory sessionFactory;
       
        private Class<T> persistentClass;
    
        protected AbstractHibernateDAO(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        this.persistentClass = (Class<T>) ((ParameterizedType)
                    getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        }

    Line 36 is bolded on this snippet for NominationHibernateDAO:

    Code:
    @Repository("nominationDao")
    public class NominationHibernateDAO<T extends Nomination>
            extends AbstractHibernateDAO<T,Integer>
            implements NominationDAO<T> {
    
    
          @Autowired(required = true)
          public NominationHibernateDAO(SessionFactory sessionFactory) {
               super(sessionFactory);
          }
    I guess I should have worded the title differently, my apologies. Please let me know if this is an adequate place to post this problem or if I should list it elsewhere, I am kinda blocked at the moment and it doesn't help that I have limited and very little knowledge of both Hibernate AND Spring.
    Last edited by nimchip; Jan 18th, 2012, 03:46 PM.

    Comment


    • #3
      You're running into Type erasure problems. T, in your case, is not a reifiable object either in NominationHibernateDAO or AbstractHibernateDAO.

      The way I've run around this issue (not very good, but at least it works) is to pass in the class in the constructor.

      Comment


      • #4
        Thank you, I'll try this.


        edit: I solved this by simply making NominationHibernateDAO an abstract class (as was intended), and having a new HibernateDAO bean for a generic nomination, this way I can extend from NominationHibernateDAO comfortably while still keeping a generic version. Super simple but of course sometimes it's tough to see what's going on. Thanks a lot for your help.
        Last edited by nimchip; Jan 19th, 2012, 12:05 PM.

        Comment

        Working...
        X