persistence@glassfish.java.net

Re: Serializable Entities

From: Craig L Russell <Craig.Russell_at_Sun.COM>
Date: Wed, 21 Nov 2007 12:43:18 -0800

Hi Shelley,

I guess you're referring to this:
Rule #2: The object to be persisted must mark all nonserializable
fields transient

This rule only applies to nonserializable fields. So your persistent
fields should not be marked as transient.

In your case, just implement readObject and writeObject and the
default serialization behavior is not used.

Better still, implement readExternal and writeExternal which gives
you even more control over the process.

Craig

On Nov 21, 2007, at 12:21 PM, Shelley wrote:

> I am using field-based access for my entities and would like to
> implement custom serialization [1] (which involves marking all
> instance variables as transient and overriding the default
> serialization protocol by specifying the
> writeObject(ObjectOutputStream out) and readObject(ObjectInputStream
> in)). As mentioned, the fact that JPA does not persist transient
> variables is the reason for my question - is it common practice to use
> default serialization for entities, or do I need to use property-based
> access so that I can write my own custom serialization?
>
> [1] http://java.sun.com/developer/technicalArticles/Programming/
> serialization/
>
>
> On Nov 21, 2007 2:05 PM, Stephen Connolly
> <stephen.alan.connolly_at_gmail.com> wrote:
>> instance variables marked transient will neither be serialized nor
>> persisted by JPA.
>> what are you keeping in these transient fields that cannot be derived
>> from the non-transient fields?
>>
>>
>>
>> On 11/21/07, Shelley <randomshelley_at_gmail.com> wrote:
>>> What are the best practices for serializing entities? My
>>> entities use
>>> field-based access, so custom serialization will not work because
>>> this
>>> requires that the instance variables are declared 'transient' (which
>>> indicates to the persistence provider that such fields are not to be
>>> persisted). Is default serialization generally a recommended
>>> practice
>>> for entities, or will I need to refactor my entity classes for
>>> property-based access in order to customize the serialization?
>>>
>>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell_at_sun.com
P.S. A good JDO? O, Gasp!