Hi again,

I finally solved my problem ;-)

As I told you, I have extended the ListDataModel class to provide it
with a callback mechanism to be able to reattach any object to the
current Hibernate session.

Let me show you the code:

// Provide the callback within the constructor.
public LazyListDataModel(List list, DataModelCallback dataModelCallback)
{
        super(list);

        this.dataModelCallback = dataModelCallback;
}

// Overwritten method to call the provided callback method.
public Object getRowData()
{
        // Get the object...
        Object object = super.getRowData();

        // ...and call the callback method before returning the object.
        if (this.dataModelCallback != null)
        {
                this.dataModelCallback.execute(object);
        }

        return object ;
}

Then, the DataModelCallback is a simple interface:

public interface DataModelCallback
{
        public abstract void execute(Object object);
}

And finally, I simply use this special lazy implementation of the
DataModel in my JSF bean:

this.workingDataModel = new LazyListDataModel(this.searchData(), new
DataModelCallback()
{
        public void execute()
        {
                // Call Hibernate's lock stuff...
        }
}

It works like a charm :-)

2005/6/20, Werner Punz <[EMAIL PROTECTED]>:
> Enrique Medina wrote:
> > Hi,
> >
> > Maybe this is not the most adequate forum to post this question, but I
> > know many of you are using MyFaces together with Spring & Hibernate,
> > so it would be great it you give me some comments.
> >
> > The problem I have comes with Lazy collections in my objects. Assuming
> > you know how to use Hibernate & Spring, the problem comes when I try
> > to paginate my DataTable using the DataScroller, because the
> > OpenSessionInViewFilter pattern establishes the session Hibernate in
> > the initial query (the one that shows the first page of the
> > DataTable). So, when the following page is needed, as MyFaces executes
> > a new request, the session binded by the OpenSessionInViewFilter for
> > this new request, is not obviously the same as the session previously
> > established at the first request.
> >
> > Finally, I always get a Lazy initialization exception from Hibernate,
> > as you surely have imagined...
> >
> Ok sorry for the wild guessing in the last mails, I just read up
> the spring docs, and I now have a clearer picture of things. I think I
> can point you towards a definitive solution now.
> 
> The lazy loading and the old objects BINDING approach you follow is
> rather unnecessary if you use a datamodel instead of feeding old objects
> into the session (probably via a collection you have stored somewhere in
> the session scope).
> 
> > So my question is very simple:
> >
> > How do you manage objects with lazy collections being shown in
> > different request with respect to the OpenSessionInViewFilter pattern?
> >
> > Thanks in advance. I'm looking forward to hearing your comments.
> >
> > Enrique Medina.
> >
> 
> O think I know where your problems is, you try to recycle the old
> objects in a new session which is opened automatically.
> The objects are probably stored in a session scoped collection
> and then fed back into the model after the request (which is caused
> by the paging), theoretically you could use bind to lock them to the new
> session, but that is problematic and not really necessary.
> 
> The best way to solve your problem if you dont want to go the shale
> route (which basically does the same as the Spring class in a safer way)
> is, just to implement a pure datamodel,
> you get the session from spring anyway, and feed the data from the
> methods you have to implement from the model, just as you guessed.
> 
> The call apprach probably will go like that
> Constructor of the model... request the session from spring, which is
> already opened
> The next callbacks are for delivering the number of rows etc...
> then load the object row by row as the outer system requests it from the
> model and you are set...
> 
> For performance increase you probably can
> set an iterator on the give me the number of rows methods
> and then recycle that one for each subsequent, give me the next row
> request. That way it should work.
> 
> Sorry for the guessing from my side in the last mails. I overlooked the
> Spring part.
> 
> Either way, you probably will have to implement a specialized datamodel.
> 
>

Reply via email to