Results 1 to 3 of 3

Thread: How to implement lazy-loading in EJB - Spring?

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Location
    Ukraine, Kyiv
    Posts
    2

    Default How to implement lazy-loading in EJB - Spring?

    I need no access spring service-layer from ejb. Inside spring I use "Open Session In View" (OSIV) pattern to access hibernate session in web views for lazy loading properties. How can I use OSIV pattern in EJB container?

    My current solution is not so good:

    Code:
    public interface AbstractCallback {
    	Object execute();
    }
    
    public class OSIVTemplate {
    	
    	private SessionFactory sessionFactory;
    	
    	protected void addInterceptor(){
    		// ...
    		// add hibernate interceptor
    		// ...
    	}
    	
    	protected void removeInterceptor(){
    		// ...
    		// remove hibernate interceptor
    		//...
    	}
    	
    	public final Object doInOSIV(AbstractCallback callback){
    		addInterceptor();
    		Object result = callback.execute();
    		removeInterceptor();
    		return result;
    	}
    	
    }
    in ejb-support class:


    Code:
    public class OrderService extends org.springframework.ejb.support.AbstractStatelessSessionBean {
    	
    	private OSIVTemplate template;
    
    	public Order getOrderById(final long id){
    		final OrderService orderService = (OrderService) getBeanFactory().getBean("orderService");
    		Order order = (Order) template.doInOSIV(new AbstractCallback(){
    			public Object execute() {
    				return  orderService.getOrderById(id);
    			}
    		});
    		return order;	
    	}
    
    	protected void onEjbCreate() throws CreateException {
    		// TODO Auto-generated method stub
    	}
    
    }
    May be there is simpler way to put interceptors on EJB...

  2. #2
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    You could create a proxy around your orderService object in your application context and add the interceptor there. Also, you should also set an OrderService member in your onEjbCreate so you are only accessing the application context once per bean instantiation, not once per operation.
    Bill

  3. #3
    Join Date
    Feb 2007
    Location
    Ukraine, Kyiv
    Posts
    2

    Default

    Thanks. I've created service layer ejb-facade with interceptor which have access to all services.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •