I do the same exact trick, but I convert a PersistentBag to an
ArrayList.
The tricky part is figuring out where to put your "copy" code so that
it doesn't get in the way or complicate your existing code. I use a
custom field serializer, which is an undocumented feature of GWT.
If you have an object, say Foo, you can customize the GWT RPC
serialization for Foo by creating a class in the same package as Foo,
called Foo_CustomFieldSerializer. Then you add two static methods to
the class as follows:
public static void serialize(SerializationStreamWriter writer, Foo
instance) throws SerializationException {
}
public static void deserialize(SerializationStreamReader reader, Foo
instance) throws SerializationException {
}
Then for every field, you call the appropriate methods in the writer
or reader to serialize or deserialize. Here's the cool part. When
you encounter a collection managed by Hibernate, you can simply swap
out the collection for a GWT safe implementation. So if Foo has a one
to many collection called bars (say it's a List), you can safely
serialize it with the following code:
public static void serialize(SerializationStreamWriter writer, Foo
instance) throws SerializationException {
...
writer.writeObject(new ArrayList(instance.getBars()));
...
}
and the deserialization is just as easy
public static void deserialize(SerializationStreamReader reader, Foo
instance) throws SerializationException {
...
instance.setBars((List)reader.readObject());
...
}
The best part about using a custom field serializer is that your
original object does not get modified. I tried an approach where I
swapped out the collection just prior to returning from my RPC
method. This caused two problems. First it complicated the code.
Second, Hibernate detected a change to my object and persisted it.
This wasn't a huge problem, but it did cause an unnecessary write to
the database.
I also looked into Hibernate4GWT. But I found that if you don't need
to do lazy loading of persistent collections on the client side, then
Hibernate4GWT is overkill.
-Greg
> >> Thanks.- Hide quoted text -
>
> - Show quoted text -