Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
21. Jul 2008, 16:05 CET | Link

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

4 Replies:
22. Jul 2008, 11:52 CET | Link

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"
				...
22. Jul 2008, 14:58 CET | Link

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

22. Jul 2008, 15:18 CET | Link

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.

 

--------------------------------------------------

Check my blog to find announcements on Seam Framework: Experience the Evolution of Java EE

22. Jul 2008, 20:22 CET | Link

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

Thanks