GAE Python Datastore - query on nested class table
Question: I have a Java class that contains some nested classes, as follow:

    
    
    public class OuterClass{
    
        @PrimaryKey
        private String id;
    
        @Persistent
        private String name;
    
        // --- blah blah
    
        static class MyNestedClass{
            //--- properties declaration
        }
    }
    

This class works fine, basically within just one structure (OuterClass) I can
store _n_ nested structures, and each structure works as a datastore table.

Inspecting my ds with `datastore viewer` I see that I have now a table called
`OuterClass$MyNestedClass`, I can successfully run GQL queries on this table
like this:

    
    
    //--- note the wrapping quotation marks on the table name
    SELECT * FROM "OuterClass$MyNestedClass" where something = '100'
    

Till now everything is OK. Now I need to create a `google app engine python`
method that empties that nested class table. I already did this with other
tables/classes, but never with nested classes.

This is my python code:

    
    
    import cgi
    import datetime
    import urllib
    import webapp2
    
    from google.appengine.ext import db
    
    class OuterClass$MyNestedClass(db.Model):
        name = db.StringProperty()
        surname= db.StringProperty()
        age = db.StringProperty()
    
    class emptyMyNestedClassHandler(webapp2.RequestHandler):
        def get(self):
            nestedclass= db.GqlQuery("SELECT __key__ FROM \"OuterClass$MyNestedClass\"")
            count = 0
    
            for p in nestedclass:
                count += 1
            db.delete(nestedclass)
    
            self.response.out.write("Deleted SMyNestedClass Entities: " + str(count))
    
    app = webapp2.WSGIApplication([
        ('/emptyMyNestedClass', emptyMyNestedClassHandler)
    ], debug=True)
    

and this is the error I'm getting

    
    
    class OuterClass$MyNestedClass(db.Model):
                    ^
    SyntaxError: invalid syntax
    

I tried to change the class name from `OuterClass$MyNestedClass` to
`MyNestedClass`, but I get this error:

    
    
    KindError: No implementation for kind 'OuterClass$MyNestedClass'
    

Which name do I have to assign to my python class in order to make it work?
How can I handle the $ issue?

hope I've been clear enough, thank you

* * *

## Solution

Following [Matt's suggestion](http://stackoverflow.com/a/26629206/1054151), my
JAVA class now looks like this:

    
    
    public class OuterClass{
    
        @PrimaryKey
        private String id;
    
        @Persistent
        private String name;
    
        // --- blah blah
        @PersistenceCapable( table="NestedClass")
        @Embedded
        static class NestedClass{
            //--- properties declaration
        }
    }
    

And this is the Python code:

    
    
    import cgi
    import datetime
    import urllib
    import webapp2
    
    from google.appengine.ext import db
    
    class NestedClass(db.Model):
        name = db.StringProperty()
        surname = db.StringProperty()
        age = db.StringProperty()
    
    class emptyNestedClassHandler(webapp2.RequestHandler):
        def get(self):
            nestedclass= db.GqlQuery("SELECT __key__ FROM NestedClass")
            count = 0
    
            for p in nestedclass:
                count += 1
            db.delete(nestedclass)
    
            self.response.out.write("Deleted NestedClassEntities: " + str(count))
    
    app = webapp2.WSGIApplication([
        ('/emptyNestedClass', emptyNestedClassHandler)
    ], debug=True)
    

Note that I **had** to declare class `NestedClass`.

Thanks!


Answer: Take a look at this resource for [JDO
annotations](http://db.apache.org/jdo/annotations.html).

you may want to annotate your class like this:

    
    
    public class OuterClass{
    
        @PrimaryKey
        private String id;
    
        @Persistent
        private String name;
    
        // --- blah blah
        @PersistenceCapable( table="NestedClass")
        @Embedded
        static class NestedClass{
            //--- properties declaration
        }
    }
    

With which from GQL you should be able to do this:

    
    
    SELECT * FROM "OuterClass" where NestedClass.something = '100'
    

