Presentation is loading. Please wait.

Presentation is loading. Please wait.

MCS 270 Spring 2014 Object-Oriented Software Development.

Similar presentations


Presentation on theme: "MCS 270 Spring 2014 Object-Oriented Software Development."— Presentation transcript:

1 MCS 270 Spring 2014 Object-Oriented Software Development

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

3 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

4 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

5 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

6 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

7 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu JDO Overview MCS 270 Object-Oriented Software Development

8 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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. MCS 270 Object-Oriented Software Development

9 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

10 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Example – PMF.java MCS 270 Object-Oriented Software Development 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 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Example – PMF class – used in GusListModel.java MCS 270 Object-Oriented Software Development public class GusListModel { static final PersistenceManagerFactory pmf = PMF.get();. }

12 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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. MCS 270 Object-Oriented Software Development

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

14 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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) MCS 270 Object-Oriented Software Development

15 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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; MCS 270 Object-Oriented Software Development

16 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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; ….. } MCS 270 Object-Oriented Software Development

17 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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 MCS 270 Object-Oriented Software Development

18 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu 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(); }} MCS 270 Object-Oriented Software Development

19 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Reading PersistenceCapable Object (GusListModel) public static List getPostData() { PersistenceManager pm = pmf.getPersistenceManager(); Query query = pm.newQuery(PostData.class); List posts = (List ) 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 (posts); } MCS 270 Object-Oriented Software Development

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

21 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu MCS 270 Object-Oriented Software Development

22 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu MCS 270 Object-Oriented Software Development

23 GUSTAVUS ADOLPHUS COLLEGEgustavus.edu Demo MCS 270 Object-Oriented Software Development


Download ppt "MCS 270 Spring 2014 Object-Oriented Software Development."

Similar presentations


Ads by Google