JPA Query to get entity type - google-app-engine

I am trying to write a JPA query to get the type for a particular entity, given the id of the entity. I have an abstract class Account, and concrete subclasses CustomerAccount and AdministratorAccount. The id is an attribute of the Account, so I am trying to construct a query to return the Type (i.e. foo.bar.CustomerAccount) given the ID of the account.
I tried the following:
String sql = "SELECT TYPE(a) from Account a where a.id = :userId";
But that doesn't seem to work. Any ideas? I'm using the google app engine jpa implementation (datanucleus) if that helps.

Firstly, FWIW you are using Google's JPA plugin which just happens to use some ancient jars provided by the DataNucleus project. You are not using DataNucleus JPA.
Secondly, the datastore "GAE/Datastore" and Google's JPA plugin are not likely to support JPQL "TYPE" since that came along after their plugin was developed.
Finally, you maybe would get the info you want in a more efficient way by just doing
Object obj = em.find(Account.class, id);
Class type = obj.getClass();
since this also inspects the L1/L2 caches

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

Update query using JPA and Google App Engine

I'm trying to update my entity class, but I am getting :
javax.persistence.PersistenceException: Only select and delete statements are supported.
this exact code worked fine when i wasn't using Google app engine.
My code is:
em.getTransaction().begin();
Query query =
em.createQuery("UPDATE Profile p" +
"SET p.age = 1 " +
"WHERE p.email = :email" );
query.setParameter("newPoint", point);
query.setParameter("email", email);
int updateCount = query.executeUpdate();
em.getTransaction().commit();
So I'm asking how can I update the entity query?
Since the GAE datastore doesn't support a query that updates fields in the datastore directly, then you similarly cannot do that via the JPA API. Since JPA was never designed for other types of datastores then you will see features that are inapplicable.
Retrieve the objects via query, and update them manually.
When GAE provides full map-reduce out-of-the-box then something like that would be viable to support

Grails: updating hibernate after externally loading data

I have a grails application. I'd like to load data into the underlying database with something external to grails, perl, specifically. I know I have to update the hibernate sequence after external data loading, otherwise on the next create object in grails, hibernate throws an exception; but is there anything else I need to update? Do I have to clear the hibernate cache, for instance? This would seem to be a very common issue, but there's no discussion of it in the grails docs. Thanks.
Found this
http://grails.1312388.n4.nabble.com/Accessing-the-2nd-level-cache-to-allow-it-to-be-cleared-via-a-controller-or-service-td1390985.html
Hibernate has APIs for this. You can get the query cache via sessionFactory.getQueryCache() and clear it using
sessionFactory.queryCache.clear()
You can access a cache for a domain classes using its full class name, e.g.
def cache = sessionFactory.getSecondLevelCacheRegion('com.foo.bar.Book')
and clear it via
sessionFactory.getSecondLevelCacheRegion('com.foo.bar.Book').clear()
You can also call evict() on the sessionFactory for an entire class
sessionFactory.evict(com.foo.bar.Book)
or for an individual instance
sessionFactory.evict(com.foo.bar.Book, 42)

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

Objectify embedded maps fail to retrieve

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.

Resources