On Tue, 2008-02-05 at 17:42 -0800, Kent Tong wrote:
> Hi,
>
> Suppose I have a session scoped bean class Bean1 that refers
> to an application scoped bean (Bean2) using a managed property:
>
> public class Bean1 implement Serializable {
> private transient Bean2 appBean;
> }
>
> The appBean field is marked as transient because as a global object, it is
> not
> serializable. However, when the Bean1 instance is deserialized, JVM will
> create the bean instance and restore the fields using brute force without
> calling
> constructors or involving JSF. It means the appBean field will be null.
>
> Any workaround? This seems to be a design fault in JSF.
This is nothing to do with JSF at all. It's the way http sessions and
serialization work.
HttpSession instances can be serialized/deserialized by the servlet
container under a number of conditions, all listed in the appropriate
specification. If an instance of Bean1 is in the session, it will be
serialized/deserialized too.
And java's serialization has *always* discarded transient objects on
serialization and marked them as null on deserialization. That's
perfectly normal. There are a number of options here for you, including
transient Bean2 appBean = new Bean2();
or accessing it only via a getBean2() method that checks whether it is
null and restores it, or implementing custom serialization for Bean1.
See a java tutorial on serialization for further information.
Regards, Simon