Google App Engine Datastore how to get entity for Key<?> - google-app-engine

I'm new to Datastore and now I'm developing Gae application with Datastore and Objectify. Entity class has the form
#Entity
public class MyClass1 {
public Key<MyClass2> field1;
#Id Long id;
and so on
}
MyClass2 has the form
#Entity
public class MyClass2 {
....
#Id public Long id;
#Index public String field2;
....
}
I have entity of MyClass1. How can I get the value of field2 ?
If I use DatastoreService1.get(myclass1.field1) I get
method get(Key) in the DatstoreService is not applicable for arguments (Key<MyClass2>)
Please help me

Your error doesn't really relate to the question, so I assume you want to get the value of field1, not field2.
The error is because the DatastoreService get() method expects a com.google.appengine.api.datastore.Key, but you are passing it a com.googlecode.objectifyKey<T>, which is an Objectify key - they're not the same thing. Here are the docs.
I would recommend using a Ref<MyClass2> as you can then just do something like:
MyClass2 myClass2 = myClass1.field1.get();
(using your code example).

Related

Parent Entity Google Cloud API

I'll explain the situation. I followed these two great tuto:
http://rominirani.com/2014/01/10/google-cloud-endpoints-tutorial-part-1/
https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial
However, they explain how create API for Google Cloud Endpoint, with entities independent from each other. So the API class looks like this:
package com.example.mobileassistant;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Offer entity.
*/
#Entity
public class Offer {
#Id
private String offerId;
private String title;
private String description;
private String imageUrl;
.... (With Getter and Setter)
}
My question is: How to create a class with a parent entity of another? Like the entitie Wheel, with Parent Car. I can not create a relationship with its parent entity (With the annotation)!
I tried this but it did not seem to work (Foreign Key):
package com.example.mobileassistant;
import javax.jdo.annotations.ForeignKey;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Offer entity.
*/
#Entity
public class Wheel {
#Id
private String Id;
#ForeignKey
private String parent_id;
private String title;
private String description;
.... (With Getter and Setter)
}
Actually, I have given up JDO because it don't really support parent entity, like I can read here: http://www.igorkromin.net/index.php/2013/03/14/jdo-youre-dumped-app-engine-and-bi-directional-relationships/
So I'm using now Objectify with Google Cloud Endpoint (For my Android App) and It works so perfect, especially with parent/child relationship !! You can read example here: http://blog.xebia.fr/2014/06/23/google-app-engine-cloud-endpoint-creer-notre-api-v2-et-lutiliser-avec-angularjs/
Why would it when you put it there?
See this page
http://www.datanucleus.org/products/accessplatform/jpa/annotations.html#ForeignKey
which explains clear enough that it is part of the JoinColumn, JoinTable, CollectionTable, SecondaryTable annotations.

Is there any way to save only one property of an Entity with Objectify?

Because the Datastore is shared across multiple versions of an application in App Engine, I'm looking into a way for saving only certain properties of an Entity.
Let's say I have the following class in version 1 of my app:
#Entity
public class ThingA {
#Id private Long id;
private String field1;
private String field2;
}
But in version 2, I changed this class to be:
#Entity
public class ThingA {
#Id private Long id;
private String field1;
private String field2;
private String field3;
}
The problem with saving the whole entity is that every time ThingA is saved on version 1 of the application, it sets "field3" to null.
It would be awesome if there's a way to save only certain fields on ThingA instead of the whole entity.
Thanks
I'm going to answer my own question after Googling a little bit more: The Datastore do not support partial updates to an entity. So that's it.

Assigning String as Google App Engine Entity Key

I searched a lot. Still I am not able to understand how to assign a String to Google App Engine Entity class id(Key).
So I have:
Key id;
And
setId(String id) {
// I know that here I can use id = KeyFactory.createKey("","");
}
But I don't know how to use createKey method here to assign my string to id. Please consider User here as my Entity class name.
P.S. I am using JPA.
Thank you.
Not sure of your entity is looking but this works well for me.,
#Entity
class Person{
#Id String personId;
String firstName;
}

Ancestor Query with Objectify not returning results

I am using objectify 3.1 on appengine and attempting to do a ancestor query. I want to fetch all the children of an object that are of a certain kind. Below is my code:
#Cached
#Entity
public class Car {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String make;
private String model;
}
#Cached
#Entity
public class Tire {
#Id
public String keyName;
#Parent Key<Car> car;
private String brand;
private String size;
}
My query is this code:
List<Tire> list= ofy.query(Tire.class).ancestor(new Key<Car(Car.class,carID))).list();
When I create the tire objects I use this code to set the relationship:
newTire.setCar(new Key<Car>(Car.class,Car.getID()));
I know the Parent relationship is there because I can query for it in the datastore admin and it shows the parent in the decoded entity key:
Decoded entity key: Car: id=135172 > Tire: name=myCarUniqueID
This query always returns 0 results, and it seems like I have followed all the best practices on the objectify website. Any help would be appreciated!
Your code looks ok, so the only thing that might be wrong is a non-existing carID.
The Objectify javadoc listed on the site is actually for trunk version which is a forthcoming Objectify 4. What you need to look at is Objectify 3.1 javadoc: this version has fetch() on the query.
Also, #GeneratedValue is not an Objectify annotation.

JDO query with base class gives null pointer

I am getting this error when running junit test
Testcase: testGet_User(Authentication.UserManagerTest): Caused an ERROR
null
java.lang.NullPointerException
at org.datanucleus.store.appengine.query.DatastoreQuery.getMappingForFieldWithName(DatastoreQuery.java:1307)
at org.datanucleus.store.appengine.query.DatastoreQuery.addLeftPrimaryExpression(DatastoreQuery.java:1107)
at org.datanucleus.store.appengine.query.DatastoreQuery.addExpression(DatastoreQuery.java:871)
at org.datanucleus.store.appengine.query.DatastoreQuery.addFilters(DatastoreQuery.java:832)
at org.datanucleus.store.appengine.query.DatastoreQuery.performExecute(DatastoreQuery.java:230)
at org.datanucleus.store.appengine.query.JDOQLQuery.performExecute(JDOQLQuery.java:89)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1489)
at org.datanucleus.store.query.Query.executeWithArray(Query.java:1371)
at org.datanucleus.jdo.JDOQuery.execute(JDOQuery.java:243)
at Authentication.UserManager.get(UserManager.java:86)
at Authentication.UserManagerTest.testGet_User(UserManagerTest.java:110)
code for get is:
public static UserBean get(User user) {
PersistenceManager pm = PMF.get();
// get user with id
Query query = pm.newQuery(UserCommon.class);
query.setFilter("id == idParam");
query.declareParameters("String idParam");
System.out.println("\t\tID:" + user.getUserId());
List<UserCommon> userDatas = (List<UserCommon>) query.execute(user.getUserId());
Where I have persistent classes that looks :
#PersistenceCapable(detachable="true")
#Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class UserCommon {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private String id;
and
#PersistenceCapable(detachable="true")
public class Professor extends UserCommon {
and
#PersistenceCapable(detachable="true")
public class Student extends UserCommon {
basically, I'd like to have 2 types of users.
but while logging in, only information I have is their id.
Hence, I was trying to query on base class instead of either Professor or Student.
However, I got NullPointerException.
any suggestions to where I've made a mistake ?
Thanks in advance !
App Engine JDO has limited support for inheritance, and no polymorphic support http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html#Polymorphic_Relationships
this includes queries.
Personally I would suggest using some other persistence wrapper rather than JDO such as http://code.google.com/p/objectify-appengine/ and http://code.google.com/p/twig-persist/
which do a better job using the advantages of GAE.

Resources