4 Replies Latest reply on Jul 22, 2008 8:22 PM by tomek szzzaaos

    entityManager as argument?

    tomek szzzaaos Newbie

      Hi,


      I'm trying to follow the dvd example, but in the search updateResults() I need to do few more things to calculate prices.



      so I have:


      
      @Stateful
      @Name("search")
      public class SearchAction implements Search, Serializable{
      
         @Begin(join = true)
         public String doSearch(){
            ...
         }
      
         public void updateResults() {
            if(this.customer!=null) {
            for(Stock stock : resultList) {
                StockPrice price = new StockPrice(stock, this.customer, new BigDecimal(1));
                price.get(this.entityManager); //get method uses at least few times entityManager to do query to db
                
                stock.setCustomerPrice(price.getItemPrice());
            }
            }
         }
      }
      



      StockPrice.get method heavily uses the entityManager, but sometimes I'm getting:


      org.hibernate.LazyInitializationException: could not initialize proxy - no Session



      the question is can I pass as argument entityManager?


      Sorry for so stupid question.


      Tomek

        • 1. Re: entityManager as argument?
          Adrien Orsier Newbie

          How do you declare your EntityManager? The error might occur because you're not using the Seam Managed Persistence Context (also refered as SMPC).
          To have a SMPC, you need to do something like
          @In
          EntityManager entityManagerName
          Where entityManagerName is declared in WEB-INF/component.xml like that


          <persistence:managed-persistence-context 
                              name="entityManagerName"
                              ...


          • 2. Re: entityManager as argument?
            tomek szzzaaos Newbie

            in components.xml I have:


            <persistence:managed-persistence-context name="entityManager"
                                                      auto-create="true"
                                                      persistence-unit-jndi-name="java:/WebshopEntityManagerFactory"/>



            Anyway just found my problem, I have created instance of Contact class in an authenticate method,


            @Name(contact)
            @Entity
            public class Contact{
                    
                    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
                    @JoinColumn(name="customer_code", referencedColumnName="code", nullable=false)
                    private Customer customer;
            
                    
            }




            and used @Out annotation to be able use it later:


                @Out(required=false, scope = ScopeType.SESSION)
                private Contact contact;



            but then when I try to call method doSearch:


              

            @Begin(join=true)
               public String doSearch


            and use in it:


               

            contact.getCustomer().getPriceLists(); //the price lists are fetched using the LAZY mode




            I'm getting the error above. Could you suggest what would be the best approach?
            - start the long running conversation in the authenticate method?
            - or start it on doSearch() and reselect Contact from db?


            Thanks for your time, and sorry for my English.


            Tomek

            • 3. Re: entityManager as argument?
              Jacob Orshalick Apprentice

              You could always merge the Contact into the SMPC when doSearch is invoked:


              contact = entityManager.merge(contact);



              or take a look at this alternative.


              Hope it helps.

              • 4. Re: entityManager as argument?
                tomek szzzaaos Newbie

                Great, I tried first solution for now and it works!


                Thanks