Objectify index is not created - google-app-engine

I try to use objectify on google app engine standart environment and get exception. My classes look like this:
#Entity
public class Company {
#Id Long id;
#Index String companyName;
public Company() {
}
public Company(Long id, String companyName) {
this.id=id;
this.companyName = companyName;
}
}
#Entity
public class CompanyProject {
#Id Long id;
#Index String projectName;
#Parent Key<Company> owner;
public String cost;
public CompanyProject() {
}
public CompanyProject(long userId, String projectName) {
this();
this.projectName = projectName;
owner = Key.create(Company.class, userId); // Creating the Ancestor key
}
}
When I query data like this:
Key<Company> theUser = Key.create(Company.class, 1);
Iterable<CompanyProject> projects = ObjectifyService.ofy().load().type(CompanyProject.class).ancestor(theUser).order("projectName").list();
I get exception
com.google.cloud.datastore.DatastoreException: no matching index found. recommended index is:
- kind: CompanyProject
ancestor: yes
properties:
- name: projectName
Without order("projectName") query works just fine. Removed all entities of this kind from datastore, than added new, still get this exception. I use Gradle, not Maven if this matters. Maybe should be extra build step to create indexes or smth.

You are filtering on multiple properties (well, ancestor counts as 'another property') so you need a multi-property index defined in datastore-indexes.xml.
See the official documentation: https://cloud.google.com/appengine/docs/standard/java/config/indexconfig

Related

Objectify One-to-Many and Many-To-One in Google App Engine

I'm creating simple backend on Google App Engine using Objectify, that can store Users and thier Recipes.
Model look like this:
User has many Recipes, every Recipe has one author.
I want to be able to:
Get list of Recipes with authors in it
Get list of Users without fetching all recipes
Get one User with all his recipes
According to guide I have done this:
#Entity
public class User {
#Id
private Long id;
#Index
private String name;
List<Recipe> recipes = new ArrayList<>();
/* Other fields */
}
and Recipe :
#Entity
public class Recipe {
#Id
Long id;
String name;
#Index
#Load
Ref<User> author;
/* Other fields */
}
I save Recipe with author (User object) in it.
and 2. requirements work fine,
but when I try to get User with all recipes like this:
public User get(#Named("id") long id) throws NotFoundException {
User user = ofy().load().type(User.class).id(id).now();
if (user == null) {
throw new NotFoundException("Could not find User with ID: " + id);
} else {
List<Recipe> recipes = ofy().load().type(Recipe.class).filter("author", user).list();
account.recipes = recipes;
}
return account;
}
I get empty recipe list.
What am I doing wrong ?
Your author property in your Recipe is a Ref, which is essentially a Key, so you need to filter on that key, like this:
List<Recipe> recipes = ofy().load().type(Recipe.class).filter("author =", Key.create(user)).list();
Incidentally, instead of using .now(), checking for null, then throwing an exception, you could use .safe() which does the same thing for you.

Objectify Delete doesn't seem to be working

I'm trying to delete an entity from my datastore using objectify but doesn't seem to be deleted even after shutting down the instance and restarting it. This is what the entity looks like in the datastore (both when it's on the production server & dev server):
This is the code i'm using to try and delete it:
#ApiMethod(name = "deleteDataVersion")
public Result deleteDataVersion(#Named("id") String id) {
// Where id is the id of the entity in the datastore.
if (id != null && !id.equals("")) {
ofy().delete().type(DataVersion.class).id(id).now();
return new Result(Result.STATUS_SUCCESS);
} else
return new Result(Result.STATUS_FAILED);
}
I've also tried this code:
#ApiMethod(name = "deleteDataVersion")
public Result deleteDataVersion(#Named("id") String id) {
if (id != null && !id.equals("")) {
// DataVersion doesn't have a parent.
Key<DataVersion> key = Key.create(null, DataVersion.class, id);
ofy().delete().key(key).now();
return new Result(Result.STATUS_SUCCESS);
} else
return new Result(Result.STATUS_FAILED);
}
But the entity never gets deleted. This is the code for my entity:
#Entity
public class DataVersion {
#Id
private Long id;
String folderName;
#Index
String effective;
public DataVersion() {
}
public DataVersion(String folderName, String effective ) {
this.folderName= folderName;
this.effective = effective;
}
// Getters & setters..
}
I just can't seem to find the problem :( Any help would be greatly appreciated! I'm sure it's something minor I'm overlooking (fairly new to Objectify/AppEngine).
The ID you have in parameter in your Endpoint is a String, and you try to delete the object DataVersion where the ID is a Long.
ofy().delete().type(DataVersion.class).id(Long.valueOf(id)).now();
would work better !
First get the key.
Key<DataVersion> key = Key.create(null, DataVersion.class, id);
Then fetch the entity from the database using the key.
DataVersion dataVersion = ofy().load().key(key).now();
Then delete the entity using objectify.
ofy().delete().entity(dataVersion).now();

How do I query a single field in AppEngine using JDO

I've got a Product POJO that looks like.
#PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Product extends AbstractModel {
#Persistent
private String name;
#Persistent
private Key homePage;
#Persistent
private Boolean featured;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Key getHomePage() {
return homePage;
}
public void setHomePage(Key homePage) {
this.homePage = homePage;
}
public boolean isFeatured() {
return featured;
}
public void setFeatured(Boolean featured) {
this.featured = featured;
}
}
My DataStore is currently completely empty.
I'd like to retrieve all homePage keys where featured is true for the Product.
I'm trying
PersistenceManager persistenceManager = getPersistenceManager();
Query query = persistenceManager.newQuery("SELECT homePage FROM " + getModelClass());
query.setFilter("featured == true");
List<Key> productPageKeys = (List<Key>) query.execute();
However this is giving me a null pointer error. How should I be constructing this query?
Cheers,
Peter
To do a projection, you would do something like
Query q = pm.newQuery("SELECT myField FROM mydomain.MyClass WHERE featured == true");
List<String> results = (List<String>)q.execute();
where String is the type of my field. Any basic JDO documentation would define that.
Internally GAE/J will retrieve the Entity, and then in the post-processing before returning it to the user it is manipulated into the projection you require.
As Nick pointed out in the other reply, this gives no performance gain over doing it yourself ... but then the whole point of a standard persistence API is to shield you from such datastore-specifics of having to do such extraction; it's all provided out of the box.
Entities are stored as serialized blobs of data in the datastore, so it's not possible to retrieve and return a single field from an entity. You need to fetch the whole entity, and extract the field you care about yourself.

Grails constraints GORM-JPA always sorting alphabeticaly

In my grails app, in which I use GORM-JPA, I cannot define the order of the elements of the class using the constraints. If I autogenerate the views, they are all sorted alphabetically, instead of the defined order. Here's my source class:
package kbdw
import javax.persistence.*;
// import com.google.appengine.api.datastore.Key;
#Entity
class Organisatie implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Long id
#Basic
String naam
#Basic
String telefoonnummer
#Basic
String email
#Basic
OrganisatieType type
#Basic
String adresLijnEen
#Basic
String adresLijnTwee
#Basic
String gemeente
#Basic
String postcode
#Basic
String faxnummer
static constraints = {
id visible:false
naam size: 3..75
telefoonnummer size: 4..18
email email:true
type blank:false
adresLijnEen size:5..250
adresLijnTwee blank:true
gemeente size: 2..100
postcode size: 4..10
faxnummer size: 4..18
}
}
enum OrganisatieType {
School,
NonProfit,
Bedrijf
}
The variable names are in Dutch, but it should be clear (Organisatie = organisation, naam = name, adres = address, ...).
How do I force the app to use that order of properties? Do I need to use # annotations?
Thank you!
Yvan
(ps: it's for deploying on the Google App Engine ;-) )
Try installing and hacking scaffolding, and use DomainClassPropertyComparator in your gsp-s. Scaffold templates do a Collections.sort() on default comparator, but you can use explicit one.
The absence of Hibernate might be the cause: without it, DomainClassPropertyComparator won't work, and Grails uses SimpleDomainClassPropertyComparator - I'm looking at DefaultGrailsTemplateGenerator.groovy
You can, for sure, provide another Comparator that will compare the order of declared fields.
EDIT:
For example, after installing scaffolding I have a file <project root>\src\templates\scaffolding\edit.gsp. Inside, there are such lines:
props = domainClass.properties.findAll{ ... }
Collections.sort(props, comparator. ... )
where comparator is variable provided by Grails scaffolding. You can do:
props = ...
Collections.sort(props, new PropComparator(domainClass.clazz}))
where PropComparator is something like
class PropComparator implements Comparator {
private Class clazz
PropComparator(Class clazz) { this.clazz = clazz }
int compare(Object o1, Object o2) {
clazz.declaredFields.findIndexOf{it.name == o1}
- clazz.declaredFields.findIndexOf{it.name == o2}
}
}

Map null column as 0 in a legacy database (JPA)

Using Play! framework and it's JPASupport class I have run into a problem with a legacy database.
I have the following class:
#Entity
#Table(name="product_catalog")
public class ProductCatalog extends JPASupport {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer product_catalog;
#OneToOne
#JoinColumn(name="upper_catalog")
public ProductCatalog upper_catalog;
public String name;
}
Some product catalogs don't have an upper catalog, and this is referenced as 0 in a legacy database. If I supply the upper_catalog as NULL, then expectedly JPA inserts a NULL value to that database column.
How could I force the null values to be 0 when writing to the database and the other way around when reading from the database?
I don't see any easy way of achieving what you want with JPA directly (and there are great chance that even if you find a way that works with basic operation like save or load, that it will not work with more complex use case, like complex criteria / hql, none standard fetching mode, etc)
So i would do that :
#Entity
#Table(name="product_catalog")
public class ProductCatalog extends JPASupport {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer product_catalog;
#Column(name="upper_catalog")
public Long upper_catalog_id;
public String name;
public ProductCatalog getUpperCatalog() {
if (upper_catalog_id == 0)
return null;
return ProductCatalog.findById(upper_catalog_id);
}
public void setUpperCatalog(ProductCatalog pc) {
if (pc == null) {
upper_catalog_id = 0;
}
else {
if (pc.id == null) {
// option 1. a bit like a cascade
pc.save();
// option 2. if you consider passing a transient entity is not valid
throw new RuntimeException("transient entity " + pc.toString());
}
upper_catalog_id = pc.id;
}
}
}
I see two options:
Use a primitive data type as Id (i.e. int instead of Integer)
If you are using Hibernate as JPA provider, use a CustomType to do the conversion

Resources