You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Michael Chandler <Mi...@onassignment.com> on 2013/08/21 01:01:29 UTC

Wicket serialization concerns

I could use some input from some of the more experienced Wicket users out there.  I'm having a hard time fully understanding when to worry about Wicket attempting to serialize domain model objects, resulting in serialization warnings/errors in the logs.  I had been under the impression that if I had an object as a field in a Wicket component, I needed to be on alert for serialization issues.  So, if I have an object as a field, I make it a LoadableDetachableModel.  In cases where I need to load in a wired object, I make use of the handy @SpringBean annotation.  Otherwise, I assumed I was in the clear but I'm seeing now that's not the case.

In a method within a webpage component, I'm creating a List collection of domain objects for use in a form's DropDownChoice component.  Unless I put that list in a LoadableDetachableModel, I will get a serialization error.  I'm led to believe that creating a reference to any of my domain classes within a Wicket component (as a field or in a method implementation) will expose me to a serialization error unless I put it in a Wicket model.  Am I correct about this?

Thanks!

Mike Chandler



Re: Wicket serialization concerns

Posted by Carl-Eric Menzel <cm...@wicketbuch.de>.
On Tue, 20 Aug 2013 23:01:29 +0000
Michael Chandler <Mi...@onassignment.com> wrote:
> I'm led to believe that creating a
> reference to any of my domain classes within a Wicket component (as a
> field or in a method implementation) will expose me to a
> serialization error unless I put it in a Wicket model.  Am I correct
> about this?

Basically yes. Any non-transient field in your Component will be
serialized. That includes final local variables or final
method/constructor parameters if you reference those from within an
inner class. That will make the compiler generate a hidden field to
keep that reference for the inner class.

Carl-Eric

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


RE: Wicket serialization concerns

Posted by Michael Chandler <Mi...@onassignment.com>.
Michael,

This is extremely helpful.  Thanks for the detailed explanation!

Mike

-----Original Message-----
From: Michael Mosmann [mailto:michael@mosmann.de] 
Sent: Wednesday, August 21, 2013 11:53 PM
To: users@wicket.apache.org
Subject: Re: Wicket serialization concerns

Am 21.08.13 01:01, schrieb Michael Chandler:
> .. will expose me to a serialization error unless I put it in a Wicket model.  Am I correct about this?
>
There is nothing special about the serialization in Wicket. Wicket uses the default Java serialization. So every field not marked as transient will be serialized. As Martin said: watch out for non static inner classes or anon classes because you can not see the field which is created for any reference to something from the outer scope.

To put your stuff behind a model has a benefit: if wicket is "done" with your page (response is written to the client) it will throw away anything changeable from your models. In your case the list of the entries from your database is flushed from memory.

I would not recommend to put long living data into your components (which then must be serialize-able). You should put something like this into a cache.

You can put everything needed to load you date from somewhere into an model.

public class Customers extends LoadableDetachableModel<List<Customer>> {
   @SpringBean
   CustomersDao dao;

   public Customers() {
     Injector.get().inject(this);
   }

   public List<Customer> load() {
     return dao.allCustomers();
   }
}

This way your component must not deal with anything else .. it will use model which provides the data:)

Michael:)

---------------------------------------------------------------------
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: Wicket serialization concerns

Posted by Michael Mosmann <mi...@mosmann.de>.
Am 21.08.13 01:01, schrieb Michael Chandler:
> .. will expose me to a serialization error unless I put it in a Wicket model.  Am I correct about this?
>
There is nothing special about the serialization in Wicket. Wicket uses 
the default Java serialization. So every field not marked as transient 
will be serialized. As Martin said: watch out for non static inner 
classes or anon classes because you can not see the field which is 
created for any reference to something from the outer scope.

To put your stuff behind a model has a benefit: if wicket is "done" with 
your page (response is written to the client) it will throw away 
anything changeable from your models. In your case the list of the 
entries from your database is flushed from memory.

I would not recommend to put long living data into your components 
(which then must be serialize-able). You should put something like this 
into a cache.

You can put everything needed to load you date from somewhere into an model.

public class Customers extends LoadableDetachableModel<List<Customer>> {
   @SpringBean
   CustomersDao dao;

   public Customers() {
     Injector.get().inject(this);
   }

   public List<Customer> load() {
     return dao.allCustomers();
   }
}

This way your component must not deal with anything else .. it will use 
model which provides the data:)

Michael:)

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