java - Google App Engine datastore Entity not being deleted -


i using google app engine datastore store 4 string values. string vlaues added datastore in servlet:

datastoreservice datastore = datastoreservicefactory.getdatastoreservice();          entity balances;         key primarykey;         string table = "maintable";         string name = "values";          primarykey = keyfactory.createkey(table, name);          transaction t = datastore.begintransaction();             // if 'table' exists - delete         datastore.delete(primarykey);             // make sure it's deleted/         t.commit();          t = datastore.begintransaction();              balances = new entity("balances", primarykey);         updatebalances(balances);         datastore.put(balances);              // save new data         t.commit();         resp.sendredirect("/balance.jsp"); 

i want able update 4 string values each time servlet run - why key first , delete it. use separate transaction ensure happens.

the key found , deleted , values added. when load .jsp file retrieves values number of 'records' in entity grows 1 each time. not understand why record not being deleted.

here .jsp code:

  <%         datastoreservice datastore = datastoreservicefactory.getdatastoreservice();          key guestbookkey = keyfactory.createkey("maintable", "values");          query query = new query("balances", guestbookkey);          list<entity> greetings = datastore.prepare(query).aslist(fetchoptions.builder.withlimit(5));     %> <!-- should 1, gorws each time servlet hit.-->     <%= greetings.size() %> 

solution

i don't know problem code in original question. achieved objective of persisting string values across sessions on google app engine (gae) using library called objectify (http://code.google.com/p/objectify-appengine/) - aims simplify use of datastore on gae.

the library .jar file , can added java project in eclipse easily. did not find using library easy use...the main problem registering class models data wish save. registration can done once!

to register class once added listener web app registered class objectify framework , created 4 random numbers , saved them:

public class mylistener implements servletcontextlistener {     public void contextinitialized(servletcontextevent event) {              // register account class, once!         objectifyservice.register(account.class);          objectify ofy = objectifyservice.begin();         account balances = null;              // create values wish persist.         balances = new account(randomnum(), randomnum(), randomnum(),                 randomnum());              // save values.         ofy.put(balances);         assert balances.id != null;    // id autogenerated     }      public void contextdestroyed(servletcontextevent event) {         // app engine not invoke method.     }      private string randomnum() {         // returns random number string     } } 

.. code run once when server starts - happen needed modify web.xml add:

<listener>         <listener-class>.mylistener</listener-class>     </listener> 

then had .jsp page read saved values:

<% objectify ofy = objectifyservice.begin(); boolean data = false; // value "mykey" hard coded account class enter code here  // since wanted access same data every time. account = ofy.get(account.class, "mykey"); data = (null!=a); %> 

here account class:

import javax.persistence.*;  public class account {     @id string id = "mykey";     public string balance1, balance2, balance3, balance4;      private account() {}      public account(string balance1, string balance2, string balance3, string balance4)     {         this.balance1 = balance1;         this.balance2 = balance2;         this.balance3 = balance3;         this.balance4 = balance4;     } } 

one last thing...i found objectify documentation helpful in understanding gae datastore irrespective of objectify framework

for future reference, think original example failed because of line:

balances = new entity("balances", primarykey);

this doesn't create entity primarykey, creates entity primarykey ancestor key. automatically generated id every time store it.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -