You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Kogel, Jonck-van-der" <jo...@bmw.nl> on 2010/02/18 14:30:14 UTC

Ajax and OpenSessionInViewFilter problems

Hi All,
As the subject reads, I'm having some problems with Ajax and the
OpenSessionInViewFilter. Here's my basic setup:
 
I've got a page with some address info, such as street/city/zipcode.
Let's call the current value of these fields V1. There is also an ajax
link to open a modal panel that allows the user to look up street/city
info based on the zipcode. When the modal panel opens the info V1 is
passed on to the modal panel as initial value. Let's say here the user
enters new info and a street/city is found, we'll call this info V2. The
user then presses the submit button on the modal panel and indeed V2 is
now shown on the main page. However, when I now click the ajax link to
open the modal panel again, the user get's shown V1 in the modal panel
as initial filling. When finally submitting the page V2 is persisted, so
the information is being retained somewhere.
 
I have debugged the application and I see the setters of my domain
object being called. Also when debugging however I see that the load()
method of the LoadableDetachableModel is being called when I hit the
ajax link, so it seems like a new version of the object is being
retrieved from the database. I thought that's what the
OpenSessionInViewFilter was for, to allow for multiple requests in your
view without Hibernate closing/opening the session each time and
retrieving fresh copies of your objects. Here is some relevant code:
 
OpenSessionInView setup in web.xml:
 
<filter>
 <filter-name>opensessioninview</filter-name>
 <filter-class>
  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
 </filter-class>
 <init-param>
  <param-name>singleSession</param-name>
  <param-value>true</param-value>
 </init-param>
</filter>
 
Submit link on my modal panel:
 
searchResultsHolder.add(new AjaxSubmitLink("selectMatch") {
 @Override
 protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  Address address = addressModel.getObject();
  address.setZipcode(foundZipcode.getZipcode());
  address.setHouseNr(foundZipcode.getHouseNr());
  address.setStreet(foundZipcode.getStreet().getStreetName());
  address.setCity(foundZipcode.getCity().getCityName());
  address.setCountry(Country.NL);
  address.setAddressValidated(true);
  
  for (Component component : updateList) {
   target.addComponent(component);
  }
  modalWindow.close(target);
 }
});
 
The LoadableDetachableModel used (and is being called when I hit the
ajax link):
 
final IModel<ARF> mergedModel = new LoadableDetachableModel<ARF>() {
 @Override
 protected ARF load() {
  return arfService.load(arfId);
 }
};
 
Many thanks for any help!
 
Kind regards, Jonck

Re: Ajax and OpenSessionInViewFilter problems

Posted by Bert <ta...@gmail.com>.
I had/have this problem too. Right now i use serializable hibernate objects
together with an IModel implementation that does not detach the object.

Object are serialized into the session and when the user presses the save
button the objects are reattached  to the current hibernate session
and committed.

bert

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Ajax and OpenSessionInViewFilter problems

Posted by Martin Sachs <sa...@gmail.com>.
Hi ,

if you use LDM for a form Model.getObject will load the Objects from DB
in each request. This is not a Problem, if all data are in the Form
allready.

Ajax-submit sends the Form incl. all field as parameters, if valid set
it to this object and then you have to Store it in this request.  If you
want to collect datas in a transient (not persistent) way you could use
the Session, or Serializable objects.  The state of data is always in
the form or component.

I use Hibernate with serializable objects AND Lazy-loading. This is not
always a nice solution. To work with this Objects, i reattach the
hibernatesession on each method call via AspectJ. This allows to use
DB-Mapped serialized POJOs with Lazy loading in e.g. PropertyModels.

I would not recommend to use DTOs, but in some cases this is a cheap
solution.

regards
    Martin



Kogel, Jonck-van-der schrieb:
> Hi All,
> Follow-up to my own post.
>
> I found the following here:
> http://old.nabble.com/Wicket---No-Serializable-objects-Web-application-t
> d19351608.html
>
> Quote: "However , there is a problem if you use loadabledetachable with
> AJAX requests
> on your page. Model.detach() is called on every request, which causes
> your
> object to be retrieved from database on the next Model.getObject() call.
> This
> means you lose all changes on your bound domain obect on every ajax
> request.
> I found to solutions to this, and i like neither of them:
>
> a) use DTOs, which means a lot of code duplication.
>
> b) write the current object state  to the database on every detach,
> which
> means you might store objects in an incomplete state, before the user
> explicitly tells the application to store what she or hehas entered.
>
> If anybody has a nicer solution to this, i'd love to hear about it. "
>
> Option b) is definitely a no-go, so it seems I'll have to use DTO's
> then. If someone knows of a nicer solution I would also love to hear
> about it. For now I guess I'll go the DTO road.
>
> Thanks, Jonck 
>
> -----Original Message-----
> From: Kogel, Jonck-van-der [mailto:jonck-van-der.kogel@bmw.nl] 
> Sent: donderdag 18 februari 2010 14:30
> To: users@wicket.apache.org
> Subject: Ajax and OpenSessionInViewFilter problems
>
> Hi All,
> As the subject reads, I'm having some problems with Ajax and the
> OpenSessionInViewFilter. Here's my basic setup:
>  
> I've got a page with some address info, such as street/city/zipcode.
> Let's call the current value of these fields V1. There is also an ajax
> link to open a modal panel that allows the user to look up street/city
> info based on the zipcode. When the modal panel opens the info V1 is
> passed on to the modal panel as initial value. Let's say here the user
> enters new info and a street/city is found, we'll call this info V2. The
> user then presses the submit button on the modal panel and indeed V2 is
> now shown on the main page. However, when I now click the ajax link to
> open the modal panel again, the user get's shown V1 in the modal panel
> as initial filling. When finally submitting the page V2 is persisted, so
> the information is being retained somewhere.
>  
> I have debugged the application and I see the setters of my domain
> object being called. Also when debugging however I see that the load()
> method of the LoadableDetachableModel is being called when I hit the
> ajax link, so it seems like a new version of the object is being
> retrieved from the database. I thought that's what the
> OpenSessionInViewFilter was for, to allow for multiple requests in your
> view without Hibernate closing/opening the session each time and
> retrieving fresh copies of your objects. Here is some relevant code:
>  
> OpenSessionInView setup in web.xml:
>  
> <filter>
>  <filter-name>opensessioninview</filter-name>
>  <filter-class>
>   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
>  </filter-class>
>  <init-param>
>   <param-name>singleSession</param-name>
>   <param-value>true</param-value>
>  </init-param>
> </filter>
>  
> Submit link on my modal panel:
>  
> searchResultsHolder.add(new AjaxSubmitLink("selectMatch") {  @Override
> protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
>   Address address = addressModel.getObject();
>   address.setZipcode(foundZipcode.getZipcode());
>   address.setHouseNr(foundZipcode.getHouseNr());
>   address.setStreet(foundZipcode.getStreet().getStreetName());
>   address.setCity(foundZipcode.getCity().getCityName());
>   address.setCountry(Country.NL);
>   address.setAddressValidated(true);
>   
>   for (Component component : updateList) {
>    target.addComponent(component);
>   }
>   modalWindow.close(target);
>  }
> });
>  
> The LoadableDetachableModel used (and is being called when I hit the
> ajax link):
>  
> final IModel<ARF> mergedModel = new LoadableDetachableModel<ARF>() {
> @Override  protected ARF load() {
>   return arfService.load(arfId);
>  }
> };
>  
> Many thanks for any help!
>  
> Kind regards, Jonck
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


RE: Ajax and OpenSessionInViewFilter problems

Posted by "Kogel, Jonck-van-der" <jo...@bmw.nl>.
Hi All,
Follow-up to my own post.

I found the following here:
http://old.nabble.com/Wicket---No-Serializable-objects-Web-application-t
d19351608.html

Quote: "However , there is a problem if you use loadabledetachable with
AJAX requests
on your page. Model.detach() is called on every request, which causes
your
object to be retrieved from database on the next Model.getObject() call.
This
means you lose all changes on your bound domain obect on every ajax
request.
I found to solutions to this, and i like neither of them:

a) use DTOs, which means a lot of code duplication.

b) write the current object state  to the database on every detach,
which
means you might store objects in an incomplete state, before the user
explicitly tells the application to store what she or hehas entered.

If anybody has a nicer solution to this, i'd love to hear about it. "

Option b) is definitely a no-go, so it seems I'll have to use DTO's
then. If someone knows of a nicer solution I would also love to hear
about it. For now I guess I'll go the DTO road.

Thanks, Jonck 

-----Original Message-----
From: Kogel, Jonck-van-der [mailto:jonck-van-der.kogel@bmw.nl] 
Sent: donderdag 18 februari 2010 14:30
To: users@wicket.apache.org
Subject: Ajax and OpenSessionInViewFilter problems

Hi All,
As the subject reads, I'm having some problems with Ajax and the
OpenSessionInViewFilter. Here's my basic setup:
 
I've got a page with some address info, such as street/city/zipcode.
Let's call the current value of these fields V1. There is also an ajax
link to open a modal panel that allows the user to look up street/city
info based on the zipcode. When the modal panel opens the info V1 is
passed on to the modal panel as initial value. Let's say here the user
enters new info and a street/city is found, we'll call this info V2. The
user then presses the submit button on the modal panel and indeed V2 is
now shown on the main page. However, when I now click the ajax link to
open the modal panel again, the user get's shown V1 in the modal panel
as initial filling. When finally submitting the page V2 is persisted, so
the information is being retained somewhere.
 
I have debugged the application and I see the setters of my domain
object being called. Also when debugging however I see that the load()
method of the LoadableDetachableModel is being called when I hit the
ajax link, so it seems like a new version of the object is being
retrieved from the database. I thought that's what the
OpenSessionInViewFilter was for, to allow for multiple requests in your
view without Hibernate closing/opening the session each time and
retrieving fresh copies of your objects. Here is some relevant code:
 
OpenSessionInView setup in web.xml:
 
<filter>
 <filter-name>opensessioninview</filter-name>
 <filter-class>
  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
 </filter-class>
 <init-param>
  <param-name>singleSession</param-name>
  <param-value>true</param-value>
 </init-param>
</filter>
 
Submit link on my modal panel:
 
searchResultsHolder.add(new AjaxSubmitLink("selectMatch") {  @Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  Address address = addressModel.getObject();
  address.setZipcode(foundZipcode.getZipcode());
  address.setHouseNr(foundZipcode.getHouseNr());
  address.setStreet(foundZipcode.getStreet().getStreetName());
  address.setCity(foundZipcode.getCity().getCityName());
  address.setCountry(Country.NL);
  address.setAddressValidated(true);
  
  for (Component component : updateList) {
   target.addComponent(component);
  }
  modalWindow.close(target);
 }
});
 
The LoadableDetachableModel used (and is being called when I hit the
ajax link):
 
final IModel<ARF> mergedModel = new LoadableDetachableModel<ARF>() {
@Override  protected ARF load() {
  return arfService.load(arfId);
 }
};
 
Many thanks for any help!
 
Kind regards, Jonck


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org