Objectify embedded maps fail to retrieve - google-app-engine

I am using Objectify as a data access layer in my GoogleAppEngine hosted application.
The problem comes when I try to persist a map. My bean looks like this:
#Entity
#Cached
class MyBean{
#Id
private Long id;
#Embedded
Map<String, String> parameters = new HashMap<String, String>();
public MyBean(){}
//getters and setters below
}
First of all note that the map 'parameters' is not private, it was throwing a JRE exception.
When saving the map everything goes well. When retreiving it from the DataStore it fails.
My workaround is to use the #Serialized annotation. This is just a workaround since what I want to acheive is to use the expando feature of GAE Datastore.
According to the objectify doc I'm doing the right operations.
Exception details:
Caused by: java.lang.NullPointerException at
com.googlecode.objectify.impl.Transmog.loadSingleValue(Transmog.java:364)
at
com.googlecode.objectify.impl.load.EmbeddedMapSetter.safeSet(EmbeddedMapSetter.java:65)
at
com.googlecode.objectify.impl.load.CollisionDetectingSetter.set(CollisionDetectingSetter.java:37)
at
com.googlecode.objectify.impl.Transmog.loadSingleValue(Transmog.java:359)
at com.googlecode.objectify.impl.Transmog.load(Transmog.java:340) at
com.googlecode.objectify.impl.ConcreteEntityMetadata.toObject(ConcreteEntityMetadata.java:203)
at
com.googlecode.objectify.impl.QueryImpl$ToObjectIterator.translate(QueryImpl.java:668)
at
com.googlecode.objectify.impl.QueryImpl$ToObjectIterator.translate(QueryImpl.java:657)
at
com.googlecode.objectify.util.TranslatingIterator.next(TranslatingIterator.java:35)

Embedded maps were poorly supported in Objectify3, and should not have been publicly announced. The section on #Embedded Map has been removed from the Objectify3 documentation.
Objectify4 supports maps extensively, including these expando-style maps:
Map (or any primitive)
Map (key references)
Map (embedded classes
In addition, there is a #Mapify annotation that lets you take a normal collection of objects, pick one property out as a key, and store that as a Map.
Unfortunately Objectify4's documentation is not ready at this time. However, the source code is in active use by several parties. If you feel daring, build from trunk.

I also recommend using Objectify 4 - I've upgraded my app and found it fairly easy to do. I much prefer the support for fields of the type Map in particular.
To answer the question, you should never put #Embedded onto an array containing only primitives. So you don't need to specify #Embedded on your map because String is primitive in the Google App Engine Datastore.

Related

Why does the google appengine api datastore Entity not have an empty constructor?

When I pass an entity from ios to a backendAPI method that expects an entity I get the error "... No suitable constructor found for type [simple type, class com.google.appengine.api.datastore.Entity] ..." Why does the google appengine api datastore Entity not have an empty constructor? Also, is there a way for me to provide a suitable constructor other than using objectify? I tried using objectify and was able to get rid of the error but then the values of all my properties were deleted and set to null.
(I'm assuming you're using Java language)
There is no empty-constructor because to create an Entity you need to specify a Key or a Kind-Id/Name
Source: https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Entity

java.lang.IllegalArgumentException: can't accept class com.veersoft.gwt.shared.trailbalance.TrailBalanceClassResult as a memcache entity

Inorder to make my application faster, I'm using the MemcacheService. Now, while I'm trying to put an object into the MemcacheService, I'm getting the following error:
java.lang.IllegalArgumentException: can't accept class com.veersoft.gwt.shared.trailbalance.TrailBalanceClassResult as a memcache entity.
Details about the entity:
It's a normal Bean which implements IsSerializable interface.
It is not a dataStore entity. I'm using this just to render at the browser side(i.e., the deferred task will calculate the results and will send these results to the browser).
To put any object in memcache, it needs to implement the Serializable interface. IsSerializable is an anachronism from an old version of GWT and does not affect java serialization in any way.

Persisting Blobs using Objectify

Although I know there is already a Blob service for Appengine, I want to experiment on storing big blobs within the datastore.
Basically I am trying to persist this object using Objectify:
BigBlob.java
BigBlobFragments.java
However, appengine is complaining that: "BigBlobFragment is not a supported property type"
For the BigBlob type I created a DAO class with CRUD operation and registed the type like this:
static {
ObjectifyService.register(BigBlob.class);
//ObjectifyService.register(BigBlobFragment.class);
}
protected BigblobDaoImpl() {
super(BigBlob.class);
}
I actually also have tried registering BigBlobFragment.
Hopefully someone can share some ideas on how to actually persist Big blobs and fragments using Objectify.
I have not used Blobs myself but I noticed you have #PersistenceCapable above your entities... that should be #Entity.
import com.googlecode.objectify.annotation.Entity;
Then you should be able to register your entity with Objectify.
ObjectifyService.register(BigBlob.class);
You need to register both the BigBlob and the BigBlobFragment and replace all of your JDO annotations with Objectify annotations (assuming you are using ofy4. You might also want to consider embedding the BigBlobFragment object inside of the BigBlob for performance using #Embed.

JDO and Google App Engine datastore: key representation in code

I have just finished watching the following videos in an attempt to
understand JDO and Google App Engine datastore:
http://www.youtube.com/watch?v=2jW2iSKDipY
http://www.youtube.com/watch?v=Yl_J-UYE94w
http://www.youtube.com/watch?v=pzctc48c0BM
http://www.youtube.com/watch?v=tx5gdoNpcZM
Now I wonder, take the example where we have an entity of kind Grandparent
having an entity of kind Parent having an entity of kind Child as in one of
the videos. The key for one of the the Child entities could be:
Grandparent:Jane/Parent:Jack/Child:Joe
How do I code the class for this instance in JDO (presumably there will be
three classes) but I would like to see an implementation where we can see
the key values set as part of the key explicitly. Any ideas?
I also wonder, what is the difference between using JDOQL to access my
data and iterating through the various instances using iterators programmatically?
Thanks,
John Goche
There a more concrete example in the App Engine datastore java documentation: Child Objects and Relationships
You can construct Datastore queries using JDOQL string syntax and by calling methods on Query objects, there is no difference when accessible the data both will return a Collection, you can see more concrete examples in Introducing Queries in JDO

GWT: Where (how) to define POJOs to make em available for client and server? (and to use datastore on serverside)

I try to get an application running which should interact with a server via RPC (JDO in Google DataStore). So I defined a persistent POJO on the server-side to get it put in the datastore via the PersistenceManager (as shown in the gwt rpc tuts). Everything works fine. But I am not able to receive the callback POJO on the client side because the POJO is only defined on server-side. How can I realize it, that the client knows that kind of object??
(sry for my bad english)
Lars
Put your POJOs in a separate package/directory (e.g. com.example.common) and then add source declaration to your GWT module descriptor (xyz.gwt.xml):
<source path="common"/> //relative to your xyz.gwt.xml location
GWT compiler will then also compile POJOs and they will be seen by your other GWT code.
Edited:
#Lars - now I understand your problem. As I see it you have several options:
If possible use Objectify instead of JDO. Objectify uses pure POJOs and they play nicely with GWT. I use this in my projects. One nice thing that Objectify gives you is #PostLoad & # PrePersist on methods to run some code before/after POJOs are loaded/saved to datastore. I use this to handle serialization of GeoPoint for instance.
Use JDO and make copies of your domain classes. This is a pain but it would work. Use 'transient' java keyword in your server JDO classes to exclude fields you do not want to RPC.
Edit #2: There is a third option that you might prefer:
Create "fake" JDO annotation classes using super-sourcing. This is a common technique to replace classes with a GWT version. Described here: http://fredsa.allen-sauer.com/2009/04/1st-look-at-app-engine-using-jdo.html
You can use DTO(stackoverflow, moar) for transferring data to client.
Basic sample here (method getTenLatestEntries() in your case).
Or you can use some third-party libraries like objectify and stop worry about making DTO`s.

Resources