7 Replies Latest reply on Jul 4, 2008 1:56 AM by Bill Evans

    Hibernate query at startup

    Ricardo Campos Newbie

      Olá!


      Environment



      • JBoss AS 4.2.2.GA

      • JBoss Seam 2.0.2.SP1



      Configuration


      Although I'm using JBoss AS 4.2.2.GA, I'm not using an EJB3 configuration but an Hibernate standalone configuration as follows:


      <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
      <persistence:managed-hibernate-session auto-create="true" name="hibernateSession" session-factory="#{hibernateSessionFactory}"/>
      <transaction:hibernate-transaction session="#{hibernateSession}"/>



      Problem


      I'm trying to execute an Hibernate Criteria query on the application's startup. For that, I created this component:


      @Startup
      @Name("initializer")
      @Scope(ScopeType.APPLICATION)
      public class Initializer {
      
        @In
        private Session hibernateSession;
      
        @Create
        public void init() {
          List<User> users = hibernateSession.createCriteria(User.class).list();
          ...
        }
      
      }



      The problem is that Seam tries to establish it's Hibernate security by using Identity. This exception is thrown:


      java.lang.IllegalStateException: No active session context
              at org.jboss.seam.security.Identity.instance(Identity.java:115)
              at org.jboss.seam.security.EntityPermissionChecker.checkEntityPermission(EntityPermissionChecker.java:78)
              at org.jboss.seam.security.EntityPermissionChecker.checkEntityPermission(EntityPermissionChecker.java:63)
              at org.jboss.seam.security.HibernateSecurityInterceptor.onLoad(HibernateSecurityInterceptor.java:36)
              at org.hibernate.event.def.DefaultPreLoadEventListener.onPreLoad(DefaultPreLoadEventListener.java:18)
              at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:125)
              at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
              at org.hibernate.loader.Loader.doQuery(Loader.java:729)
              at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
              at org.hibernate.loader.Loader.doList(Loader.java:2220)
              at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
              at org.hibernate.loader.Loader.list(Loader.java:2099)
              at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
              at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
              at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
              at br.com.loteriaweb.servicos.utilitarios.ParametrosSistema.construir(ParametrosSistema.java:32)
              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:21)
              at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
              at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
              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:166)
              at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
              at br.com.loteriaweb.servicos.utilitarios.ParametrosSistema_$$_javassist_0.construir(ParametrosSistema_$$_javassist_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:21)
              at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
              at org.jboss.seam.Component.callComponentMethod(Component.java:2092)
              at org.jboss.seam.Component.callCreateMethod(Component.java:2015)
              at org.jboss.seam.Component.newInstance(Component.java:1976)
              ... 140 more



      I imagine the exception occurs because no user has yet accessed the application (due to the component being constructed at startup)...


      Any suggestions?


      Attempts



      • I've tried to use @Observer("org.jboss.seam.postInitialization") instead of @Startup + @Create, but no luck.

      • Identity.setSecurityEnabled(false) doesn't seem a good solution, because I would be disabling the whole application's security, wouldn't I?