1 / 23

MCS 270 Spring 2014

MCS 270 Spring 2014. Object-Oriented Software Development. MCS 270 Object-Oriented Software Development. Today ’ s schedule. GWT Persistence - JDO. MCS 270 Object-Oriented Software Development. Persistence.

nhu
Download Presentation

MCS 270 Spring 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MCS 270 Spring 2014 Object-Oriented Software Development

  2. MCS 270 Object-Oriented Software Development Today’s schedule GWT Persistence - JDO

  3. MCS 270 Object-Oriented Software Development Persistence Persistence is the ability of an object to survive the lifecycle of the process in which it resides. Objects that “die“ with the end of a process are called transient Web Apps - storage is primarily on the server side. (Cookies can be set on client, but not guaranteed to persist) CRUD - create, read, update and delete basics of persistence process

  4. MCS 270 Object-Oriented Software Development Google Web Toolkit Storage Options Java Data Objects (JDO) A standardized way of storing objects Create an object, store it, load it later Java Persistence API (JPA) Another standardized way of storing objects JPA is basically a refinement of JDO As well supported in GAE as JDO

  5. MCS 270 Object-Oriented Software Development JDO Overview Java Data Objects (JDO) is a standard interface for storing objects containing data into a database. The standard defines interfaces for annotating Java objects, retrieving objects with queries, and interacting with a database using transactions

  6. MCS 270 Object-Oriented Software Development JDO Overview An application that uses the JDO interface can work with different kinds of databases without using any database-specific code, including relational databases, hierarchical databases, and object databases. Note: JDO is Not an object database May use conventional RDBMS, OODB, etc- datastore

  7. MCS 270 Object-Oriented Software Development JDO Overview

  8. MCS 270 Object-Oriented Software Development Eclipse and JDO: GWT Plug-In: JDO and DataNucleus App Engine plugin JARs (datanucleus) placed in app's war/WEB-INF/lib/ directory. Build process performs post-compilation "enhancement" step on compiled data classes (PostData) to associate them with the JDO implementation.

  9. MCS 270 Object-Oriented Software Development PersistenceManager Web app interacts with JDO (Storage) using an instance of the PersistenceManager class. Use PersistenceManagerFactory to get PM. PersistenceManagerFactory instance takes time to initialize, – an app should reuse a single static instance. – to enforce this, an exception is thrown if the app instantiates more than one PersistenceManagerFactory

  10. MCS 270 Object-Oriented Software Development Example – PMF.java package edu.gac.mcs270.hvidsten.guslist.server; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManagerFactory; public final class PMF { private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional"); private PMF() {} public static PersistenceManagerFactory get() { return pmfInstance; } }

  11. MCS 270 Object-Oriented Software Development Example – PMF class – used in GusListModel.java public class GusListModel { static final PersistenceManagerFactory pmf = PMF.get(); . . . }

  12. MCS 270 Object-Oriented Software Development Defining Data Classes for Storage JDO is used to store plain Java data objects ("Plain Old Java Objects" or "POJOs") in the datastore. Each object made persistent with the PersistenceManager becomes an entity in the datastore. Code Annotations tell JDO how to store and recreate instances of your data classes.

  13. MCS 270 Object-Oriented Software Development .java Java Compiler .class Byte code enhancement JDO Enhancer JDO MetaData (XML) .class

  14. MCS 270 Object-Oriented Software Development Code Annotations PersistenceCapable- The interface that all persistent objects must implement. Annotated before class definition. Persistent– Designates that field of class will be stored PrimaryKey– Unique identifier of class Unowned– Designates an unowned 1-to-1 relationship (i.e. class that is a member of another class, but will not disappear when parent class disappears)

  15. MCS 270 Object-Oriented Software Development Example @PersistenceCapable(identityType=IdentityType.APPLICATION) public class PostData implements Serializable { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) // Needed for RPC communication of PostData class to server @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true”) private String id; @Persistent private String title="no title"; @Persistent private String description="empty"; @Persistent private double price=0.0;

  16. MCS 270 Object-Oriented Software Development Example (cont’d) // Need to define the Seller and Buyer as "Unowned" child objects, // as they do not disappear when PostData object is deleted. @Persistent @Unowned private Seller seller; @Persistent @Unowned private Buyer buyer; ….. }

  17. MCS 270 Object-Oriented Software Development Enabling Group Transactions Default JDO mode: one group stored (persisted) at one time. PostData has a child Seller that is unowned – different group. JDO configuration – can be set to allow more than one group (up to 5) to be stored in a single transaction, a so-called “cross-group” transaction. Add to src/META-INF/jdoconfig.xml <property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true" />

  18. MCS 270 Object-Oriented Software Development Storing PersistenceCapable Object (GusListModel) public static void storePost(PostData post) { PersistenceManager pm = pmf.getPersistenceManager(); // Transactions lock all records in a datastore and keep them locked // until they are ready to commit their changes. try { pm.currentTransaction().begin(); pm.makePersistent(post); pm.currentTransaction().commit(); } finally { if (pm.currentTransaction().isActive()) pm.currentTransaction().rollback(); if (!pm.isClosed()) pm.close(); }}

  19. MCS 270 Object-Oriented Software Development Reading PersistenceCapable Object (GusListModel) public static List<PostData> getPostData() { PersistenceManager pm = pmf.getPersistenceManager(); Query query = pm.newQuery(PostData.class); List<PostData> posts = (List<PostData>) query.execute(); // Child classes are loaded "lazily" - not until they are accessed. // To make sure they are loaded before the PersistenceManager closes, // we reference them here so they are forced to load. for(PostData post: posts){ post.getSeller(); post.getBuyer(); } return new ArrayList<PostData>(posts); }

  20. MCS 270 Object-Oriented Software Development Inspecting the Datastore After running an instance of the web app, go to to http://localhost:8888/_ah/admin/ Browse through the data - The Full Key is shown GWT pug-in (Eclipse) generates persistence files in war directory

  21. MCS 270 Object-Oriented Software Development

  22. MCS 270 Object-Oriented Software Development

  23. MCS 270 Object-Oriented Software Development Demo

More Related