Download Slides - Gustavus Adolphus College

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
MCS 270 Spring 2014
Object-Oriented Software Development
MCS 270 Object-Oriented Software Development
Today’s schedule
GWT Persistence - JDO
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
JDO Overview
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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;
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Example – PMF class – used in GusListModel.java
public class GusListModel {
static final PersistenceManagerFactory pmf = PMF.get();
.
.
.
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
.java
Java
Compiler
.class
Byte code enhancement
JDO
Enhancer
JDO MetaData
(XML)
.class
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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)
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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;
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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;
…..
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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" />
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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();
}}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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);
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
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
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Demo
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu