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

    entityManager as argument?

    tomek szzzaaos Newbie


      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:

      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

      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.


        • 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
          EntityManager entityManagerName
          Where entityManagerName is declared in WEB-INF/component.xml like that


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

            in components.xml I have:

            <persistence:managed-persistence-context name="entityManager"

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

            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:


               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.


            • 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!