3 Replies Latest reply on Apr 24, 2009 1:18 PM by giannisapi

    Use criteria in a seam action class ?

      Hi all,


      I have made an application using seam-gen . What i  want to do is use hibernate criteria instead of a query. I find it difficult because no matter when I try i cant get it to work.


      Below is the method that i want to use criteria in:


       public void queryCars() {
      
                carsList = new ArrayList<Car>();
                 
                 EntityManagerImpl entityManagerImpl = (EntityManagerImpl)  entityManager.getDelegate();
                   session = entityManagerImpl.getSession();
                   Criteria criteria = session.createCriteria(Car.class);
                
                if (make != null) {
                     System.out.println("query: " + query);
                     criteria.add(Restrictions.eq("brandFK", make));
      
                }
      
      
               
               List<Car> cars= new ArrayList<Car>();;
               carsList.addAll(criteria.list()/*q.getResultList()*/);
               for (Car car : carsList) {
                    car.setKivika(1000);
                    cars.add(car);
               }
      
               carsList = new ArrayList<Car>();
               carsList.addAll(cars);
               System.out.println("motorcyclesList size is1 : " + carsList.size());
          }
          
      



      I found this in a tutorial but it does not even compile.


      If you could somehow assist me. I know i dont give many infos here(because am lost) but if u can, please help.


      Thanks in advance.


      Regards,
      giannis


      Click HELP for text formatting instructions. Then edit this text and check the preview.

        • 1. Re: Use criteria in a seam action class ?
          ralf

          Hey,


          i faced the problem too and after some tryouts i found a solution.


          Try that:


          HibernateSessionProxy proxy = (HibernateSessionProxy) this.entityManager.getDelegate();
          Criteria criteria = proxy.createCriteria( MyClass.class );



          Does it work??


          Ralf

          • 2. Re: Use criteria in a seam action class ?
            gonorrhea

            Seam in Action:


            When you’re using JPA, you can always get down to the provider interface by calling
            the getDelegate() method on the EntityManager instance. But you have to perform
            a cast that makes an assumption about the underlying JPA provider:
            
            Session session = (Session) entityManager.getDelegate();
            
            You can define a factory in Seam to hide the cast:
            
            <factory name="hibernateSession" value="#{entityManager.delegate}"
            auto-create="true"/>
            
            You then use the @In annotation to inject the value of this factory into your component:
            
            @In private Session hibernateSession;

            • 3. Re: Use criteria in a seam action class ?

              Hi all,


              First of all thanks for the replies.


              Sorry for the late post but my pc motherboard burnt out so i had to replace it...


              The proxy thing seems to be working for me. But this:


              '<facory name="hibernateSession" value="#{entityManager.delegate}"
              auto-create="true"/>
              '





              I dont really understand it . So i will have to use sessionproxy.


              just one more question. Performance wise, which of the two sollutions iis better?



              Regards,
              gianis