0 Replies Latest reply on Nov 13, 2005 6:42 PM by binario

    EJB3 and Hibernate Querying - at my wits end!

    binario

      Hi all,

      I would really appreciate some help here. I've been stuck on this problem for ages and I'm at my wits end with it now.

      I've got the following setup on JBoss 4.0.3:

      Struts Actions --> EJB3 Session Facade --> EJB3 Entity Beans.

      I'm using the hibernate query api to query from the web tier (struts) to the backend.

      Here's the code in the session bean:

      @Stateless
      public class MyManagerBean implements MyManager {
       @PersistenceContext
       private EntityManager manager;
      
       public int searchItemsCount(DetachedCriteria query){
       Session session = ((HibernateSession) this.manager).getHibernateSession();
       int count = (Integer) query.getExecutableCriteria(session).uniqueResult();
       return count;
       }
      }
      


      And here's the relevant code in the struts action:

      
      ...
      DetachedCriteria query = this.createSearchQuery(ItemState.RUNNING,itemPostedAfter);
      
      ...
      
      private DetachedCriteria createJobSearchQuery (ItemState itemState, Date itemPostedAfterDate)
      {
      DetachedCriteria query = DetachedCriteria.forClass(Item.class); if(itemState!=null)
      {query.add(Expression.eq("itemState",itemState.ordinal()));}
      if(itemPostedAfterDate !=null)
      {query.add(Expression.ge("startTime",itemPostedAfterDate.getTime()));}
      return query;
      }
      
      


      My persistence.xml file contains the following:

      
      <entity-manager>
       <name>jobsite</name>
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/DefaultDS</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
      <!--
       <property name="hibernate.show_sql" value="true" />
      -->
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
      
       <property name="hibernate.cache.provider_class"
       value="org.jboss.ejb3.entity.TreeCacheProviderHook"/>
       <property name="hibernate.treecache.mbean.object_name"
       value="jboss.cache:service=EJB3EntityTreeCache"/>
       </properties>
      </entity-manager>
      
      


      However I'm getting the following exception:


      
      23:38:18,495 WARN [RequestProcessor] Unhandled Exception thrown: class javax.ejb.EJBException
      23:38:18,495 ERROR [[action]] Servlet.service() for servlet action threw exception
      javax.ejb.EJBException: null; CausedByException is:
       java.lang.Integer
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
       at ...
      
      java.lang.ClassCastException: java.lang.Integer
       at org.hibernate.type.EnumType.nullSafeSet(EnumType.java:121)
       at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:155)
       at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
       at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
       at org.hibernate.loader.Loader.doQuery(Loader.java:661)
       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
       at org.hibernate.loader.Loader.doList(Loader.java:2147)
       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
       at org.hibernate.loader.Loader.list(Loader.java:2021)
      ...
      


      Note that ItemState is a persisted enum:

      public enum ItemState implements Serializable {
       EMPTY, RUNNING, FINISHED
      };
      



      PLEASE can someone help me here?

      thanks so much,
      Brian