0
votes

I am embedding an entity within another entity using Objectify. I am setting certain fields to null before I embed and save, because I have the full entity stored elsewhere and I just store the embedded entity redundantly so I don't have to get() it explicitly. [Yes, I know get() is reasonably cheap, but it is still too expensive in my case, so I'd rather have everything within a single record.]

In the datastore (local) the embedded entity looks like this:

<EmbeddedEntity [User(6702622882922496)]: status = null (unindexed) avatar = fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/c127.37.466.466/s160x160/284313_181267978606379_897018_n.jpg?oh=07d9502745b9b4d80e5ed52fb46d8b24&oe=54C4D556&__gda__=1422892826_7eb452a4b837d668572e5053c5936be8 (unindexed) displayName = null (unindexed) version = 1414555328756 (unindexed) > (unindexed)

As you can see, Objectify explicitly stores the null fields.

I know I can work around this problem by serializing/JSONing/whatever the record myself, but I would rather not.

I remember to have read that datastore distinguishes between null and empty, and I was wondering if I can control the behavior how Objectify serializes an embedded entity.

Any cheap workarounds? Loadgroups? Any drawbacks that you can see where what I am doing could lead to problems, like for example when such a "barebone" entity is sent over REST and cached?

1

1 Answers

2
votes

Put this annotation on your fields:

@IgnoreSave(IfNull.class)
private String yourField;

This will prevent nulls from being stored. There are other If conditions you can use, or you can write your own. @Index and @Unindex also accept these conditions.