Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Datastore with JDO 1. Setting up DataNucleus Access Platform 2. JDO class enhancement 3. POJOs and JDO Annotations 4. PersistencyManager and its.

Similar presentations


Presentation on theme: "Using Datastore with JDO 1. Setting up DataNucleus Access Platform 2. JDO class enhancement 3. POJOs and JDO Annotations 4. PersistencyManager and its."— Presentation transcript:

1 Using Datastore with JDO 1. Setting up DataNucleus Access Platform 2. JDO class enhancement 3. POJOs and JDO Annotations 4. PersistencyManager and its Factory 5. Creating and saving objects 6. JDOQL Queries

2 Setting up the system for JDO JDO is a spec (like JPA) for persistent objects. DataNucleus is a specific JDO implementation used by Google AppEngine (like Hibernate) To use it, jdoconfig.xml is created in the final WAR file Eclipse GAE plugin users: automatically. Manually – see http://code.google.com/appengine/docs/java/gettingsta rted/usingdatastore.html

3 JDO Class enhancement Automatic post-compilation step based on the annotations you add in your classes Allows more flexibility (transparent persistance : no need to change your classes) Links your classes and the actual JDO (DataNucleus) persistency providing layer

4 POJO and JDO annotations JDO spec supports persistency for plain java objects. JDO annotations: tags that tell JDO how to store/retrieve your objects Example.

5 @PersistenceCapable public class Greeting { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key key; @Persistent private User author; @Persistent private String content; @Persistent private Date date; public Greeting(User author, String content, Date date) { this.author = author; this.content = content; this.date = date; } public Key getKey() { return key; } public User getAuthor() { return author; } public String getContent() { return content; } } 3 properties : author, content, date Private fields (important) + access through get/set functions !(why ?) JDO entity key is required Object  JDO Entity @PersistenceCapable annotation @Persistent /@ NotPersistent Some field types are persistent by default (see DataNucleus manual) Inheritance, collections and most other intuitive stuff are supported by JDO. For esoteric situations, consult with DataNucleus manual Your object relationship will affect DB operations (cascade delete)

6 PersistencyManager and its Factory All datastore operations go through PersistencyManager which is accessed through a PersistencyManagerFactory PMF takes a long time to start – maintain a singleton instance over your application

7 package guestbook; public class SignGuestbookServlet extends HttpServlet { private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName()); public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); String content = req.getParameter("content"); Date date = new Date(); Greeting greeting = new Greeting(user, content, date); PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(greeting); } finally { pm.close(); } resp.sendRedirect("/guestbook.jsp"); } } The annotations and bytecode enhancement take it from there. Once makePersistent() returns, the new object is stored in the datastore.

8 JDOQL Queries Perform queries aboud JDO entities Use newquery() method of PersistencyManager to get a query obj. The execute() method of the query object runs the query and returns a List<> of results JDOQL is a subset of SQL. For more details, see manual Example follows:

9 PersistenceManager pm = PMF.get().getPersistenceManager(); String query = "select from " + Greeting.class.getName(); List greetings = (List ) pm.newQuery(query).execute(); if (greetings.isEmpty()) { %> The guestbook has no messages. An anonymous person wrote: wrote: <% } } pm.close();


Download ppt "Using Datastore with JDO 1. Setting up DataNucleus Access Platform 2. JDO class enhancement 3. POJOs and JDO Annotations 4. PersistencyManager and its."

Similar presentations


Ads by Google