Serialization

64 views
Skip to first unread message

howzito

unread,
Jan 23, 2012, 10:22:40 AM1/23/12
to Ebean ORM
First of all,

I´m new to ebeans, thanks to the stackoverflow question strategy (it
worked well for me).

I loved what I read from the manual and decided to give it a try. So
far, it´s beens easy to use, but one cenario is giving me a hard time:

I have one use case in witch an Entity must be serialized into XML,
with an specific format. I´m using XStream for that, and vanila mode
on the query did the trick of removing enhancers.

The problem is when I have to deserialize that XML to the entity,
since XStream does not call the default constructor, I end up with the
enhanced version of the entity with all enchancements null, so any
call to setProperty will throw a NullPointerException.

Xstream has a method that let you pass the instance, so I used it. I
called new MyEntity() and passed that instance to XStream, but all
related Entities (One2Many or Many2Many) have the same problem, null
for every _ebean intercept.

Is there a way to ask ebean to "attach" (populate the internal fields
_ebean) for that entity?

I can use readResolve() (http://xstream.codehaus.org/
faq.html#Serialization_initialize_transient) on the entities to
populate _ebean fields, but how should that be done?

Thanks in advance,
Raul.

Rob Bygrave

unread,
Feb 2, 2012, 6:20:16 AM2/2/12
to eb...@googlegroups.com
Hmm. You you supply the stack trace. It is not clear what is null or why.

I'd guess your beans aren't actually 'enhanced' because you are using vanilla mode ... so you are using 'dynamically generated subclasses' .... but it is not clear what is null and why.


Cheers,Rob.

howzito

unread,
Feb 3, 2012, 7:02:29 AM2/3/12
to Ebean ORM
The beans are not enhanced, because XStream is building them from XML,
using Reflection without calling the default constructor, so
_ebean_intercept and _ebean_identity are null for all objects in the
object graph (since they´re not on the XML).

How can I "enhance" those instances?

On 2 fev, 09:20, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> Hmm. You you supply the stack trace. It is not clear what is null or why.
>
> I'd guess your beans aren't actually 'enhanced' because you are using
> vanilla mode ... so you are using 'dynamically generated subclasses' ....
> but it is not clear what is null and why.
>
> Cheers,Rob.
>

howzito

unread,
Feb 7, 2012, 2:23:02 PM2/7/12
to Ebean ORM
I created a deep clone static method in the entities that copies the
field values to a new instance created with the default constructor.
So those new instances are "attached" and I could persist them. If
someone else has this kind of problem, the deep clone is an easy
workaround.

Cheers,
Raul.

Rob Bygrave

unread,
Feb 7, 2012, 3:05:18 PM2/7/12
to eb...@googlegroups.com
Hmmm. Hard to say what your problem was though - didn't see the stacktrace.

If the beans are enhanced it doesn't matter which constructor is used. If you where using 'dynamic subclasses' then you could use ebeanServer.createEntityBean(Class cls); ... to create a appropriate 'entity bean'.

howzito

unread,
Feb 7, 2012, 3:40:16 PM2/7/12
to Ebean ORM
I´m using enhanced, not 'dynamic subclasses'.

The beans are enhanced, but no constructor is used, since XStream
create the instances using Reflection without calling the
constructor.

The stack is long gone, but its easy to bring it back. I´ll do it
tomorrow and paste it so you can see.
It´s a NullPointerException on the _ebean_intercept, when you call any
setter method.

On 7 fev, 18:05, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> Hmmm. Hard to say what your problem was though - didn't see the stacktrace.
>
> If the beans are enhanced it doesn't matter which constructor is used. If
> you where using 'dynamic subclasses' then you could use
> ebeanServer.createEntityBean(Class cls); ... to create a appropriate
> 'entity bean'.
>
Reply all
Reply to author
Forward
0 new messages