views:

108

answers:

2

I'm trying to understand a line from the Google Datastore API which says:

JDO uses annotations on Java classes to describe how instances of the class are stored in the datastore as entities, and how entities are recreated as instances when retrieved from the datastore.

I'm sure this is a very basic question for most people. But I don't understand why the class object needs to be stored in the database and later retrieved. Why not instead define what you want the datastore to look like for your application and store only relevant data from your class properties as needed instead of storing the entire class or always storing the same properties repeatedly? The instance of the class contains all kinds of information that, it seems to me, does not need to be in the database - methods, class variables, instance variables - we write that stuff in our source code and we don't need to reproduce it in the database - we only need certain relevant data that the class is operating on to be stored. Right? Maybe I'm not understanding what is meant by the quoted statement. Please correct me if I have misunderstood.

+2  A: 

Why not instead define what you want the datastore to look like for your application and store only relevant data from your class properties as needed instead of storing the entire class or always storing the same properties repeatedly?

That is exactly what what you quoted actually means:

JDO uses annotations on Java classes to describe how instances of the class are stored in the datastore as entities, and how entities are recreated as instances when retrieved from the datastore.

You use annotations to say "store field X", "ignore field Y", etc.

matt b
Yes but the annotations seem like brute force - always make sure this property is stored. Well, how about only storing that property after I complete a unit of work and my app has done all the necessary checks to make sure that a database update needs to take place?
Bijou
@Bijou: Annotations describe "what" will be stored, not "when". The update will take place when needed.
OscarRyz
Who says "when needed"? JDO or the application designer? Does JDO look at the fields marked 'persistent' and automatically update the datastore whenever those fields experience a change in their values in the application? Because that seems like it should be under the control of the developer.
Bijou
You want to store some fields in the persistence layer / database conditionally? i.e. store "name" when it is not null, and don't store it when it is?
matt b
Yes, the application would check the value of 'name' and if it's not null or if it doesn't contain only numbers, then store it. The JDO annotation logic seems to be saying, whenver the value associated with any of these fields marked 'persistent' changes to any new value then let's update the database with the new value.
Bijou
Another example to belabor the point: Let's say I have a shopping cart class and I only want to store the shopping cart in the database after my customer has entered their zip code to calculate their shipping costs. I would want the applicatiton logic to determine that the time is right to store the shopping cart, not the @Persistent annotation combined with the 'intelligence' of JDO. But maybe JDO does require this application logic and won't update any values in the database until you explicitly tell it to. I'm no Java expert.
Bijou
It should work the way you expect it:From http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Updating_an_Object"Changes are persisted when the PersistenceManager is closed"
Denis Troller
Thanks Denis. That cleared it up.
Bijou
+2  A: 

The Google Datastore is not a standard relational database like SQL Server or Oracle. Entities are stored based on their definition, rather than the traditional method of creating a schema first, then mapping an object to the schema. I don't think private members or methods are stored along with the data, so it's not like the entire object, including methods, is serialized into the Datastore.

Dave Swersky
Ok, so it's more like defining a Model class in Ruby on Rails - the class corresponds to the database table and the properties defined in the model class and the types that are defined for them are mirrored as corresponding table fields. What I don't understand is when these updates take place. If I want the data to be updated only after I've completed a unit of work, these annotations make it seem that JDO is always on the lookout for a change in the values of the properties and is constantly updating the database with the new values.
Bijou
Correct. I haven't looked directly into Google Datastore, but if it's anything like AppEngine then there's an explicit call to persist the object back to the store.
Dave Swersky