4 Replies Latest reply on Jun 26, 2008 10:05 AM by paul lmc

    EJB3.0 SESSIONBEAN WORK WITH HIBERNATE EXCEPTION

    paul lmc Newbie

      DETAIL LIKE THIS:
      public class ArticleEJB extends HibernateBase implements InfoArticleUtil, ArticleEJBLocal,
      ArticleEJBRemote {

      public List getAllArticle() {
      List list = null;
      try{
      Begin();
      list = session.createQuery("from InfoArticle").list();
      if(list==null) list = new ArrayList();
      session.flush();
      }catch(Exception e){
      e.printStackTrace();
      }finally{
      Close();
      }
      return null;
      }

      }

      public class HibernateBase {
      private static SessionFactory sf = null;
      public Session session = null;
      public void Begin(){
      //在这里�以根��置文件�使用,如果是使用JDBC-》openSession(),如果使用JTA使用getCurrentSession();
      if(sf==null) Init();
      session = sf.openSession();
      }
      public void Init(){
      try{
      InitialContext ctx = new InitialContext();
      Object obj = ctx.lookup("hibernateJNDI");
      System.out.println("hibernateJNDI==============="+obj);
      if(sf==null) sf = (SessionFactory)obj;
      }catch(Exception e){
      e.printStackTrace();
      }
      }
      public void Close(){
      if(session!=null&&session.isOpen()) session.close();
      }
      }

        • 1. Re: EJB3.0 SESSIONBEAN WORK WITH HIBERNATE EXCEPTION
          paul lmc Newbie

          IN THE CONTEXTLISTENER LIKE THIS :

          public class StartUpListener implements ServletContextListener{

          public void contextDestroyed(ServletContextEvent arg0) {

          }
          public void contextInitialized(ServletContextEvent arg0) {
          try {
          InitialContext ctx = new InitialContext();
          HibernateUtil h = new HibernateUtil();
          h.Init();
          SessionFactory newsf = h.sf;
          SessionFactory oldsf = (SessionFactory)ctx.lookup("hibernateJNDI");
          System.out.println("bind hibernateJNDI~~~~~~~~~~~~~~~~~~~~~~~`");
          if(oldsf!=null) ctx.rebind("hibernateJNDI", newsf);
          else ctx.bind("hibernateJNDI", newsf);

          } catch (NamingException e) {
          e.printStackTrace();
          }
          }
          }

          • 2. Re: EJB3.0 SESSIONBEAN WORK WITH HIBERNATE EXCEPTION
            paul lmc Newbie

            HibernateUtil LIKE THIS :

            public class HibernateUtil {
            public static SessionFactory sf = null;
            public void Init(){
            if(sf == null){
            sf = new Configuration().configure().buildSessionFactory();
            }
            }

            }

            WEB.XML LIKE THIS:
            <?xml version="1.0" encoding="UTF-8"?>
            <web-app version="2.5"
            xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


            <listener-class>sysListener.StartUpListener</listener-class>

            <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>
            </web-app>

            • 3. Re: EJB3.0 SESSIONBEAN WORK WITH HIBERNATE EXCEPTION
              paul lmc Newbie

              WHEN THE WEB DEMO RUNNING THROW EXCEPTION LIKE THIS :

              21:59:09,113 INFO [STDOUT] 21:59:09,113 INFO [Environment] Hibernate 3.1.3
              21:59:09,123 INFO [STDOUT] 21:59:09,123 INFO [Environment] hibernate.properties not found
              21:59:09,133 INFO [STDOUT] 21:59:09,133 INFO [Environment] using CGLIB reflection optimizer
              21:59:09,133 INFO [STDOUT] 21:59:09,133 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
              21:59:09,384 INFO [STDOUT] 21:59:09,384 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
              21:59:09,384 INFO [STDOUT] 21:59:09,384 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
              21:59:09,824 INFO [STDOUT] 21:59:09,814 INFO [Configuration] Reading mappings from resource: domain/InfoArti
              cle.hbm.xml
              21:59:10,946 INFO [STDOUT] 21:59:10,946 INFO [HbmBinder] Mapping class: domain.InfoArticle -> info_article
              21:59:11,026 INFO [STDOUT] 21:59:11,026 INFO [Configuration] Reading mappings from resource: domain/ChanArti
              Relation.hbm.xml
              21:59:11,036 INFO [STDOUT] 21:59:11,036 INFO [HbmBinder] Mapping class: domain.ChanArtiRelation -> chan_arti
              _relation
              21:59:11,246 INFO [STDOUT] 21:59:11,246 INFO [Configuration] Reading mappings from resource: domain/InfoChan
              nel.hbm.xml
              21:59:11,276 INFO [STDOUT] 21:59:11,276 INFO [HbmBinder] Mapping class: domain.InfoChannel -> info_channel
              21:59:11,276 INFO [STDOUT] 21:59:11,276 INFO [Configuration] Configured SessionFactory: null
              21:59:11,657 INFO [STDOUT] 21:59:11,657 INFO [HbmBinder] Mapping collection: domain.InfoArticle.chanArtiRela
              tions -> chan_arti_relation
              21:59:11,657 INFO [STDOUT] 21:59:11,657 INFO [HbmBinder] Mapping collection: domain.InfoChannel.chanArtiRela
              tions -> chan_arti_relation
              21:59:11,687 INFO [STDOUT] 21:59:11,687 INFO [NamingHelper] JNDI InitialContext properties:{}
              21:59:11,687 INFO [STDOUT] 21:59:11,687 INFO [DatasourceConnectionProvider] Using datasource: java:/moreflur
              ish
              21:59:11,687 INFO [STDOUT] 21:59:11,687 INFO [SettingsFactory] RDBMS: MySQL, version: 5.0.15-nt
              21:59:11,687 INFO [STDOUT] 21:59:11,687 INFO [SettingsFactory] JDBC driver: MySQL-AB JDBC Driver, version: m
              ysql-connector-java-5.0.8 ( Revision: ${svn.Revision} )
              21:59:11,777 INFO [STDOUT] 21:59:11,777 INFO [Dialect] Using dialect: org.hibernate.dialect.MySQLDialect
              21:59:11,797 INFO [STDOUT] 21:59:11,797 INFO [TransactionFactoryFactory] Transaction strategy: org.hibernate
              .transaction.JTATransactionFactory
              21:59:11,807 INFO [STDOUT] 21:59:11,807 INFO [NamingHelper] JNDI InitialContext properties:{}
              21:59:11,807 INFO [STDOUT] 21:59:11,807 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup c
              onfigured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
              21:59:11,807 INFO [STDOUT] 21:59:11,807 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup c
              onfigured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
              21:59:11,817 INFO [STDOUT] 21:59:11,807 INFO [SettingsFactory] Automatic flush during beforeCompletion(): di
              sabled
              21:59:11,817 INFO [STDOUT] 21:59:11,817 INFO [SettingsFactory] Automatic session close at end of transaction
              : disabled
              21:59:11,817 INFO [STDOUT] 21:59:11,817 INFO [SettingsFactory] JDBC batch size: 15
              21:59:11,837 INFO [STDOUT] 21:59:11,837 INFO [SettingsFactory] JDBC batch updates for versioned data: disabl
              ed
              21:59:11,847 INFO [STDOUT] 21:59:11,837 INFO [SettingsFactory] Scrollable result sets: enabled
              21:59:11,847 INFO [STDOUT] 21:59:11,847 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): enabled
              21:59:11,847 INFO [STDOUT] 21:59:11,847 INFO [SettingsFactory] Connection release mode: auto
              21:59:11,847 INFO [STDOUT] 21:59:11,847 INFO [SettingsFactory] Maximum outer join fetch depth: 2
              21:59:11,847 INFO [STDOUT] 21:59:11,847 INFO [SettingsFactory] Default batch fetch size: 1
              21:59:11,847 INFO [STDOUT] 21:59:11,847 INFO [SettingsFactory] Generate SQL with comments: disabled
              21:59:11,857 INFO [STDOUT] 21:59:11,857 INFO [SettingsFactory] Order SQL updates by primary key: disabled
              21:59:11,857 INFO [STDOUT] 21:59:11,857 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQu
              eryTranslatorFactory
              21:59:11,867 INFO [STDOUT] 21:59:11,867 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
              21:59:11,867 INFO [STDOUT] 21:59:11,867 INFO [SettingsFactory] Query language substitutions: {}
              21:59:11,867 INFO [STDOUT] 21:59:11,867 INFO [SettingsFactory] Second-level cache: enabled
              21:59:11,867 INFO [STDOUT] 21:59:11,867 INFO [SettingsFactory] Query cache: disabled
              21:59:11,867 INFO [STDOUT] 21:59:11,867 INFO [SettingsFactory] Cache provider: org.hibernate.cache.Hashtable
              CacheProvider
              21:59:11,887 INFO [STDOUT] 21:59:11,877 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
              21:59:11,887 INFO [STDOUT] 21:59:11,887 INFO [SettingsFactory] Structured second-level cache entries: disabl
              ed
              21:59:11,907 INFO [STDOUT] 21:59:11,907 INFO [SettingsFactory] Echoing all SQL to stdout
              21:59:11,917 INFO [STDOUT] 21:59:11,917 INFO [SettingsFactory] Statistics: disabled
              21:59:11,917 INFO [STDOUT] 21:59:11,917 INFO [SettingsFactory] Deleted entity synthetic identifier rollback:
              disabled
              21:59:11,917 INFO [STDOUT] 21:59:11,917 INFO [SettingsFactory] Default entity-mode: pojo
              21:59:13,860 INFO [STDOUT] 21:59:13,860 INFO [SessionFactoryImpl] building session factory
              21:59:16,494 INFO [STDOUT] 21:59:16,494 INFO [SessionFactoryObjectFactory] Factory name: hibernateJNDI
              21:59:16,494 INFO [STDOUT] 21:59:16,494 INFO [NamingHelper] JNDI InitialContext properties:{}
              21:59:16,494 INFO [STDOUT] 21:59:16,494 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: hiber
              nateJNDI
              21:59:16,494 INFO [STDOUT] 21:59:16,494 WARN [SessionFactoryObjectFactory] InitialContext did not implement
              EventContext
              21:59:16,494 INFO [STDOUT] bind hibernateJNDI~~~~~~~~~~~~~~~~~~~~~~~`
              21:59:24,265 INFO [STDOUT] hibernateJNDI===============org.hibernate.impl.SessionFactoryImpl@1c9c556
              21:59:24,265 ERROR [STDERR] java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl
              21:59:24,265 ERROR [STDERR] at hibernate.HibernateBase.Init(HibernateBase.java:21)
              21:59:24,265 ERROR [STDERR] at hibernate.HibernateBase.Begin(HibernateBase.java:13)
              21:59:24,265 ERROR [STDERR] at ejb3.services.Article.ArticleEJB.getAllArticle(ArticleEJB.java:18)
              21:59:24,265 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              21:59:24,265 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
              9)
              21:59:24,265 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
              l.java:25)
              21:59:24,265 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
              21:59:24,265 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              12)
              21:59:24,275 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextI
              mpl.java:166)
              21:59:24,275 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3Intercept
              orsInterceptor.java:63)
              21:59:24,275 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,275 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(Tran
              sactionScopedEntityManagerInterceptor.java:54)
              21:59:24,275 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterce
              ptor.java:47)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
              21:59:24,315 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationIntercept
              or.java:95)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInsta
              nceInterceptor.java:62)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationI
              nterceptor.java:77)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3Authentica
              tionInterceptor.java:110)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.j
              ava:46)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInte
              rceptor.java:106)
              21:59:24,315 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.
              java:240)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.
              java:210)
              21:59:24,315 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.jav
              a:84)
              21:59:24,315 ERROR [STDERR] at $Proxy66.getAllArticle(Unknown Source)
              21:59:24,345 ERROR [STDERR] at EJBClient.ClientTest.getAllArticle(ClientTest.java:18)
              21:59:24,345 ERROR [STDERR] at org.apache.jsp.index_jsp._jspService(index_jsp.java:88)
              21:59:24,345 ERROR [STDERR] at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
              21:59:24,345 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
              21:59:24,345 ERROR [STDERR] at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
              373)
              21:59:24,345 ERROR [STDERR] at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
              21:59:24,345 ERROR [STDERR] at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
              21:59:24,355 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applicatio
              nFilterChain.java:290)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC
              hain.java:206)
              21:59:24,355 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.j
              ava:96)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applicatio
              nFilterChain.java:235)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterC
              hain.java:206)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.j
              ava:230)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.j
              ava:175)
              21:59:24,355 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssoc
              iationValve.java:179)
              21:59:24,355 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java
              :84)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:12
              7)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:10
              2)
              21:59:24,355 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnect
              ionValve.java:157)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.jav
              a:109)
              21:59:24,355 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)

              21:59:24,355 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
              21:59:24,355 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Htt
              p11Protocol.java:583)
              21:59:24,355 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
              21:59:24,355 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
              21:59:24,355 ERROR [STDERR] java.lang.NullPointerException
              21:59:24,355 ERROR [STDERR] at hibernate.HibernateBase.Begin(HibernateBase.java:14)
              21:59:24,355 ERROR [STDERR] at ejb3.services.Article.ArticleEJB.getAllArticle(ArticleEJB.java:18)
              21:59:24,355 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              21:59:24,355 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
              9)
              21:59:24,355 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
              l.java:25)
              21:59:24,365 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              12)
              21:59:24,365 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextI
              mpl.java:166)
              21:59:24,365 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3Intercept
              orsInterceptor.java:63)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,365 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(Tran
              sactionScopedEntityManagerInterceptor.java:54)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,365 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterce
              ptor.java:47)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,365 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
              21:59:24,365 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,365 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationIntercept
              or.java:95)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)
              21:59:24,365 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInsta
              nceInterceptor.java:62)
              21:59:24,365 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:1
              01)

              • 4. Re: EJB3.0 SESSIONBEAN WORK WITH HIBERNATE EXCEPTION
                paul lmc Newbie

                ANYONE CAN HELP ME? THANK YOU!!!!

                WHY THROW THE CLASSCASTEXCEPTION LIKE THIS:

                21:59:24,265 ERROR [STDERR]java.lang.ClassCastException:org.hibernate.impl.SessionFactoryImpl