java.io.NotSerializableException: com.googlecode.objectify.util.ResultNow

551 views
Skip to first unread message

David Fuelling

unread,
Nov 2, 2012, 12:42:02 PM11/2/12
to objectify...@googlegroups.com
Hey Jeff,

I have various @Entity classes that have properties of type com.googlecode.objectify.Ref<T>.  I'm also trying to use some of these entities with the GAE Pipeline tools, which appear to serialize input/ouput.  However, when I run my pipeline tests, I get a java.io.NotSerializableException because com.googlecode.objectify.util.ResultNow does not implement Serializable (and Result doesn't extend it).

So, my first inclination is to simply request that "com.googlecode.objectify.Result" extend Serializable, and this fixes my issue (at least locally).

That said, I'd like to confirm that having properties of type "Ref<>" in my entities is a good pattern.  Should I be using Key's instead?  From what I can see in the Objectify docs, Ref seems like a valid property type, but just in case...

Thanks for your help!
david

Jeff Schnitzer

unread,
Nov 2, 2012, 3:15:44 PM11/2/12
to objectify...@googlegroups.com
This is an interesting conundrum, and isn't solved by making Result Serializable.  Results created by fetches inherently have links to a lot of complicated internal structure (see LoadEngine.java) which can't possibly serialize well.

I just did some research and found the magic writeReplace() method for java serialization.  So I can make ResultNow serializable and cause all other Result implementations to serialize as ResultNow objects.

This is also the solution for making Lists and Sets serialize away their proxies.

Ref<?> is definitely intended for use as an entity field type - it's a good thing to do.  This is the first time serialization has come up - it will be resolved very soon.

Jeff

Jeff Schnitzer

unread,
Nov 8, 2012, 1:03:29 AM11/8/12
to objectify...@googlegroups.com
This issue should be addressed in the 4.0b1 release (and git master) but I haven't tested it.

Jeff

Rafael Dipold

unread,
Nov 8, 2012, 6:58:02 PM11/8/12
to objectify...@googlegroups.com, je...@infohazard.org
I have same problem, but when using MemcacheService. I use MemcacheService to create user login session.

I was using objectify-4.0a4.jar with concrete entity relationship and works fine. Then I replace it to objectify-4.0b1.jar and refactoring my code to use Ref<?> instead of concrete entity relationship and now I give the same problem of  David Fuelling.

Ex. (Please, this is only example, not my real code):

public class Session implements Serializable {
private static final long serialVersionUID = 1L;
private final MemcacheService cache;
        public Session(MemcacheService cache) {
this.cache = cache;
}
public void setCar(Car car) {
cache.put(keyCar, car); //java.io.NotSerializableException: com.googlecode.objectify.util.ResultNow
}
}

@Entity
class Car implements Serializable {
private static final long serialVersionUID 1L;
    @Id Long id;
   
@Load Ref<Person> driver;    // Person is an @Entity

   
public Person getDriver() { return driver.get(); }
   
public void setDriver(Person value) { driver = Ref.create(value); }
}

Jeff Schnitzer

unread,
Nov 8, 2012, 7:58:58 PM11/8/12
to Rafael Dipold, objectify...@googlegroups.com
Doh!

I forgot to make ResultNow serializable.  This has been fixed in master, and there are test cases.

Thanks for the report.

Jeff

Rafael Dipold

unread,
Dec 3, 2012, 2:52:53 PM12/3/12
to objectify...@googlegroups.com, Rafael Dipold, je...@infohazard.org
The ObjectifyImpl does not need to be serializable too?

Because how do I store or retrieve data from the datastore using objectify in a DeferredTask for example?

Best Regards,

Jeff Schnitzer

unread,
Dec 3, 2012, 4:51:21 PM12/3/12
to objectify...@googlegroups.com, Rafael Dipold
Don't try to serialize Objectify components in a DeferredTask.

If you are using the ofy() static method, you should not have any member variables of type Objectify.

Jeff

Rafael Dipold

unread,
Dec 3, 2012, 5:30:26 PM12/3/12
to objectify...@googlegroups.com, Rafael Dipold, je...@infohazard.org
Thanks for the explanation Jeff!
I guess didn't build my DAO's correctly in this case...

Rafael Dipold

unread,
Dec 7, 2012, 12:01:14 PM12/7/12
to objectify...@googlegroups.com
Just compile from the source.

Here is in attach

Em sexta-feira, 7 de dezembro de 2012 12h24min33s UTC-2, David escreveu:
I just ran into this issue. When can we expect the next release with this change?

Thanks,
 David
objectify-4.0b1.jar

Jeremy Leeder

unread,
Apr 14, 2014, 8:43:00 PM4/14/14
to objectify...@googlegroups.com, Rafael Dipold, je...@infohazard.org
I'm getting this same error in Version 4.0b3

Caused by: java.io.NotSerializableException: com.googlecode.objectify.util.ResultProxy

My code is,

List<RentalProduct> prods = ofy().load().type(RentalProduct.class).list();
req.getSession().setAttribute("products", prods);

I'm not sure why in this case the class in question is serializable.

public class RentalProduct implements Product, Serializable {

Jeff Schnitzer

unread,
Apr 14, 2014, 9:12:45 PM4/14/14
to objectify...@googlegroups.com
The problem is that the List<?> is not serializable. It's a proxy to a
real list.

Some later version of Objectify fixed this issue (made the proxy
materialize on serialization). I would upgrade to 4.1 (or 5.0 if you
are ready).

Jeff
> --
> You received this message because you are subscribed to the Google Groups
> "objectify-appengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to objectify-appen...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages