Results 1 to 2 of 2

Thread: Is it a right way to initialize in web controller?

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    5

    Default Is it a right way to initialize in web controller?

    I know that have some solutions to init lazy collection in spring(hibernate).
    Some thing like OpenSessionInView pattern or touch POJO inside DAO or transaction.
    But I want to init lazy collection in the web controller(spring MVC).
    That's my solution.

    Spring MVC:
    Code:
    public class MyFormContrller extends SimpleFormController {
    	public Map referenceData(HttpServletRequest request) {
    		Item item = service.getItem();
    		Set Bids = item.getBids(); // bids is lazy, iterate bids will cause LazyInitializtionException
    		MyUtil.initialize(item, bids); // bids is initiated, so we can use it in view (jsp)
    	}
    }
    MyUtil:
    Code:
    public class MyUtil {
    	public static void initialize(Object parent, Collection children) {
    		SessionFactory sessionFactory=  (SessionFactory) fCtx.getBean("sessionFactory");
    		Session session = sessionFactory.openSession();
    		TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    		session.update(parent);
    		Hibernate.initialize(children);
    		SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    		session = holder.getSession();
    		session.flush();
    		TransactionSynchronizationManager.unbindResource(sessionFactory);
    		SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);
    	}
    }
    Is it a right way?

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    If your code solves your problem then it's correct . OpenSessionInView is nice because you don't have to worry about lazy-init problem even if you are inside the view (Spring MVC in you case) - the session is going to be closed after the view is generated/rendered.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Similar Threads

  1. View to forward to another controller?
    By justinp in forum Web
    Replies: 6
    Last Post: Apr 2nd, 2010, 01:53 PM
  2. Question on Multi-Action Controller
    By jcheung in forum Container
    Replies: 3
    Last Post: Nov 26th, 2009, 06:58 AM
  3. Replies: 6
    Last Post: Jul 20th, 2007, 05:56 AM
  4. Controller forwaring
    By ojs in forum Web
    Replies: 5
    Last Post: Jul 11th, 2005, 02:10 AM
  5. Replies: 0
    Last Post: Sep 24th, 2004, 08:03 AM

Posting Permissions

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