7 Replies Latest reply on Oct 20, 2010 5:13 PM by Jeff Faath

    org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session

    Dahn Maier Newbie

      Hello,


      I am currently trying to get Hibernate Search running in my WAR app. In order to create the index I have the following method:



      @Transactional
          public void createIndex()
          {
               log.info("creating index") ;
               
               FullTextEntityManager ftem = Search.getFullTextEntityManager(entityManager);  
      
               try 
               {
               //     fullTextEntityManager.createIndexer().startAndWait() ;     
                    ftem.createIndexer().startAndWait() ;     
               }
               catch(InterruptedException e)
               {
                    log.info("interrupted exception" + e) ;
               }
               
          }



      However this makes the server cough with the following error when it gets to the line involving the FullTextEntityManager:



      java.lang.ClassCastException: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
           at org.hibernate.search.impl.FullTextSessionImpl.<init>(FullTextSessionImpl.java:102)
           at org.hibernate.search.Search.getFullTextSession(Search.java:46)
           at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.getFullTextSession(FullTextEntityManagerImpl.java:69)
           at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.createIndexer(FullTextEntityManagerImpl.java:290)
           at com.maierinc.stylect.session.RecordConversationAction.createIndex(RecordConversationAction.java:96)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      etc...



      I've tried on both Jboss 4.2.3 (with hibernate libraries updated) and JBoss 5.0.1. Same issue. I've also tried injecting the FullTextEntityManager



      @In
      FullTextEntityManager ftem ;



      but I get an exception claiming it's null even if I change to:



      @In(create=true)
      FullTextEntityManager ftem ;




      Google didn't give up any secrets. I'm at a loss. Help would be greatly appreciated.


      Thanks,
      Dahn




        • 1. Re: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
          Neil Richardson Newbie

          Try using




          @In
          FullTextEntityManager entityManager;
          




          If Hibernate Search is available on the classpath then Seam always wraps the EntityManager in a FullTextEntityManager before injecting it. FullTextEntityManager extends EntityManager so you can inject it as either.

          • 2. Re: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
            Rocky S Master

            Use this-


            @In(create=true, required=true, value="fullTextEntityManager")
            FullTextEntityManager ftem ;
            



            If you use ftem, seam will try inject the fullentitymanager with the following value ftem
            which does not exists.

            • 3. Re: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
              Jeff Faath Newbie

              I'm getting the same error and none of the solutions seems to work.  The differences are:


              - Using Tomcat 6


              - Using the latest snapshot build of Hibernate Search.  The reason being is this issue was just resolved:
              http://opensource.atlassian.com/projects/hibernate/browse/HSEARCH-591


              - I am not using Hibernate Search annotations but instead am programmatically mapping the tables I want to index.


              What mechanism does Seam use to detect Hibernate Search on the classpath?  Could one of theses differences be the reason for the problem?


              Thanks,


              Jeff

              • 4. Re: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
                Neil Richardson Newbie

                Seam uses the following mechanism in org.jboss.seam.persistence.HibernatePersistenceProvider to detect Hibernate Search on the classpath


                      try 
                      {
                         Class.forName("org.hibernate.search.Version");
                         hibernateSearchPresent = true;
                      }
                      catch (Exception e)
                      {
                         log.debug("Hibernate Search not present", e);
                      }
                
                



                Just noticed that Dahn's post says


                java.lang.ClassCastException: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session


                If you've got exactly the same error then it may be worth checking the imports you've got in your code to make sure that your IDE hasn't put the wrong one in by mistake.

                • 5. Re: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
                  Jeff Faath Newbie

                  I see the code in Seam where this is happening.  I should have pointed out that I was using Seam 2.2.0.GA.  The code in this version looks like this:


                           try {
                              Class searchVersionClass = Class.forName("org.hibernate.search.Version");
                              Field versionField = searchVersionClass.getDeclaredField("VERSION");
                              version = (String) versionField.get(null);
                           }
                           catch (Exception e)
                           {
                              log.debug("no Hibernate Search, sorry :-(", e);
                           }
                  



                  Which produces this error:


                  13:45:34,796 DEBUG [HibernatePersistenceProvider] no Hibernate Search, sorry :-(
                  java.lang.NoSuchFieldException: VERSION
                       at java.lang.Class.getDeclaredField(Class.java:1882)
                       at org.jboss.seam.persistence.HibernatePersistenceProvider.<clinit>(HibernatePersistenceProvider.java:61)
                       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 java.lang.Class.newInstance0(Class.java:355)
                       at java.lang.Class.newInstance(Class.java:308)
                       at org.jboss.seam.Component.instantiateJavaBean(Component.java:1438)
                       at org.jboss.seam.Component.instantiate(Component.java:1359)
                       at org.jboss.seam.Component.newInstance(Component.java:2122)
                       at org.jboss.seam.Component.getInstance(Component.java:2021)
                       at org.jboss.seam.Component.getInstance(Component.java:2000)
                       at org.jboss.seam.Component.getInstance(Component.java:1994)
                       at org.jboss.seam.Component.getInstance(Component.java:1967)
                       at org.jboss.seam.Component.getInstance(Component.java:1962)
                       at org.jboss.seam.persistence.PersistenceProvider.instance(PersistenceProvider.java:203)
                       at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:79)
                       at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:107)
                       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.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                       at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
                       at org.jboss.seam.Component.callComponentMethod(Component.java:2249)
                       at org.jboss.seam.Component.unwrap(Component.java:2275)
                       at org.jboss.seam.Component.getInstance(Component.java:2041)
                       at org.jboss.seam.Component.getInstance(Component.java:1983)
                       at org.jboss.seam.Component.getInstance(Component.java:1977)
                       at org.jboss.seam.Component.getInstanceInAllNamespaces(Component.java:2349)
                       at org.jboss.seam.Component.getValueToInject(Component.java:2301)
                       at org.jboss.seam.Component.injectAttributes(Component.java:1736)
                       at org.jboss.seam.Component.inject(Component.java:1554)
                       at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                       at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
                       at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                       at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
                       at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
                       at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
                       at com.cloudlogix.registry.config.SystemProperties_$$_javassist_seam_0.onCreate(SystemProperties_$$_javassist_seam_0.java)
                       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.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                       at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
                       at org.jboss.seam.Component.callComponentMethod(Component.java:2249)
                       at org.jboss.seam.Component.callCreateMethod(Component.java:2172)
                       at org.jboss.seam.Component.newInstance(Component.java:2132)
                       at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304)
                       at org.jboss.seam.contexts.Contexts.startup(Contexts.java:296)
                       at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278)
                       at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:116)
                       at org.jboss.seam.init.Initialization.init(Initialization.java:740)
                       at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36)
                       at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
                       at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
                       at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
                       at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
                       at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
                       at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
                       at org.apache.catalina.core.StandardService.start(StandardService.java:516)
                       at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
                       at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
                       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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
                       at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
                  



                  Which makes me think I'm using incompatible versions of Seam and Hibernate Search.  Is there a recommended Seam version to use keeping in mind that I need to use the latest snapshot of Hibernate Search?

                  • 6. Re: org.jboss.seam.persistence.HibernateSessionProxy cannot be cast to org.hibernate.classic.Session
                    Ben Catherall Newbie

                    Just found this with Search 3.3.0Beta1 on Seam 2.2.1.CR1


                    To fix, I copied the whole HibernatePersistenceProvider class, changed precedence to APPLICATION and replaced the version check with:


                             String version = null;
                             try {
                                Class searchVersionClass = Class.forName("org.hibernate.search.Version");
                                Method versionMethod = searchVersionClass.getDeclaredMethod("getVersionString");
                                version = (String) versionMethod.invoke(null);
                             }



                    Bit of a bodge, but seems to work!