1 Reply Latest reply on Jan 17, 2006 12:59 PM by shinerella

    DetachedCriteria Query

    shinerella

      Im trying to use a Detached Criteria to make my queries. But I ran into a problem. Here I describe the situation


      I have this class with the parameters to make the search, where I also define a Detached Criteria . that later on I 'll use on my session bean

      @Name("searchParameters")

      public DetachedCriteria getSearchCriteria()
       {
      
       searchCriteria = DetachedCriteria.forClass(CAROPrince.class);
      
       if (contains != null)
       {
       searchCriteria.add(Restrictions.ilike("code", contains, MatchMode.ANYWHERE));
       }
      
      return searchCriteria;
       }
      



      then I have a bean PrinceSearchAction

      
      public class PrinceSearchAction implements Serializable, PrinceSearch
       {
       @PersistenceContext(unitName = "princes")
       private Session session;
      
       @PostConstruct
       public void setSession()
       {
       HibernateUtil.setSession(session);
       }
      
       @In(required = false, create = true)
       private CAROPrince prince;
      
       @In(required = false, create = true)
       PrinceSearchParameters searchParameters;
      
       @Out(required = false)
       private List<CAROPrince> returnprinces;
      
      
       public String findPrinces()
       {
       try
       {
       DetachedCriteria searchCriteria = searchParameters.getSearchCriteria();
       Criteria crit = searchCriteria.getExecutableCriteria(session);
       crit.setMaxResults(100);
       returnprinces = crit.list();
       }
      
       if (returnprinces.size()>0)
       {
       return "found";
       }
       else
       {
       return "notfound";
       }
      
      catch ( Exception e )
       {/**
       * TODO:Implement error handeling when looking for princes in the future :)
       */
       e.printStackTrace();
       return "error";
       }
       }
      
      


      WELL, there is always problems when we search for a prince, but my problem is exacly in this line
      -> Criteria crit = searchCriteria.getExecutableCriteria(session);

      And I get the following exception :


      INFO [STDOUT] java.lang.ClassCastException: org.jboss.ejb3.entity.InjectedHibernateSession
      17:31:38,015 INFO [STDOUT] at org.hibernate.criterion.DetachedCriteria.getExecutableCriteria(DetachedCriteria.java:52)
      17:31:38,015 INFO [STDOUT] at py.com.wpg.CooPy.contractManagement.ejb.ContractSearchAction.findContracts(ContractSearchAction.java:103)
      17:31:38,015 INFO [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      17:31:38,015 INFO [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      17:31:38,015 INFO [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      17:31:38,015 INFO [STDOUT] at java.lang.reflect.Method.invoke(Method.java:585)
      17:31:38,015 INFO [STDOUT] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)


      What Im doing wrong? Is that a correct way to use the detachedCriteria?


        • 1. Re: DetachedCriteria Query
          shinerella

          Remark:

          In debugging time into the first step getSearchCriteria()

          what happens is the following

          My "contains" variable has a string value: "DEPP" , so it seems to go ok adding the restriction, and it also returns me that "searchCriteria" .