Anda di halaman 1dari 27

Java Data Objects (JDO)

Wednesday, 24 September 2008


Welcome!

 We are your assistants for this course:

 Michael Duller
michael.duller_at_inf.ethz.ch
 Ionut Subasu
ionut.subasu_at_inf.ethz.ch

Wednesday, 24 September 2008 Department of Computer Science 2


Schedule
Date Topic
25.09.2008 Setup of development environment, warm up with
JDO, transactions in a nutshell
02.10.2008 Convergence session (Java programming, tools), Q&A
09.10.2008 HTTP protocol, HTML, servlet API
16.10.2008 Enterprise Java Beans
23.10.2008 Q&A (on demand)
30.10.2008 Web Service interface to EJBs
06.11.2008 Project: TPC-W, a transactional web e-Commerce
benchmark
13/11 – 04/12/2008 Q&A (on demand)
11/12/2007 Presentation of the project results

Wednesday, 24 September 2008 Department of Computer Science 3


Getting started

 Prerequisits:
 Relational DBMS (eg. MySQL Server/Postgres)
 Latest Java SE SDK
 Eclipse 3.3 / Netbeans 6.1 IDE
 Tomcat Server
 Primary supported platform is Windows
 Mac OS X, Linux, FreeBSD,… users: use at your
own risk, some pointers on separate foil
Wednesday, 24 September 2008 Department of Computer Science
Installing Java SDK

 Download the recent release of the JDK


 http://java.sun.com/ → Downloads → Java SE
 JDK 6 Update 7 is the most recent release
 Choose Windows Offline Installation
 Install the JDK by running the installer
 Keep the default installation options
 If you already have a JDK installed
 Check for updates (Java applet in Control Panel)
Wednesday, 24 September 2008 Department of Computer Science
Installing Eclipse

 Download the recent release of Eclipse Classic


 http://www.eclipse.org/ → Downloads
 Version 3.4 is most recent
 Unzip the file to your hard disk
 Place a shortcut in your start menu, desktop,…
 Go to extracted folder and click once on eclipse.exe
 Hit CTRL-C
 Open start menu and right-click → Paste Shortcut
Wednesday, 24 September 2008 Department of Computer Science
MySQL Server

 Download the recent release of MySQL


 http://dev.mysql.com/downloads/-> MySQL Community
Server-> 5.0 -> Windows -> Without installer

 Unzip the file to a location


 Eg. C:\mysql-5.0.67-win32

 Start the server by running


 C:\mysql-5.0.67-win32\bin\mysqld.exe

Wednesday, 24 September 2008 Department of Computer Science


Testing the server

 Open a command line and go to the folder


where you have the MySQL binaries
 C:\mysql-5.0.67-win32\bin\
 Run mysql.exe and if the server is started you
should see the mysql>_ prompt

Wednesday, 24 September 2008 Department of Computer Science


MySQL GUI Tools

 Download the recent release of MySQL GUI


Tools
 http://dev.mysql.com/downloads/ -> GUI Tools
5.0 -> mysql-gui-tools-5.0-r12-win32.msi
 Follow the instructions on the screen for
installation

Wednesday, 24 September 2008 Department of Computer Science


MySQL Query Browser
 For the first connection
use:
 Server Host: localhost
 Username: root
 No password

Wednesday, 24 September 2008 Department of Computer Science


MySQL Query Browser Interface

SQL Query

Available Schemas
(Databases)

Syntax Help
Query Results

Wednesday, 24 September 2008 Department of Computer Science


Java Data Objects(JDO)

 Java Data Objects (JDO) a specification of Java object persistence.


 Offers transparent persistency services to the domain model.

 JDO persistent objects are


 ordinary Java classes;

 no requirement to implement specific interfaces or extend from special


classes.

 Classes can be persisted in a variety of ways


 e.g. to RDBMS, to OODB, to files.

Wednesday, 24 September 2008 Department of Computer Science


Example – Product collection
 Products can be Books, Cd-s,
Electronic equipment etc.
 Products can have
 Books are a specialization of a
Product
 Book extends Product

Wednesday, 24 September 2008 Department of Computer Science


Persistence of Objects
 Java Objects are
Java R-DBMS
mapped to a
relational DBMS
 Each object is a
table and has
different relations

Wednesday, 24 September 2008 Department of Computer Science


JDO Implementations - JPOX

Tutorials and info at:


 http://www.jpox.org/
 http://www.jpox.org/servlet/wiki/display/USERS/
Setting+up+JPOX+with+Eclipse

Wednesday, 24 September 2008 Department of Computer Science


Libraries
JPOX Core
JPOX Enhancer
JPOX Plugins (get rdbms for mysql, and java 1.5)
Eclipse JPOX Plugin
apache-log4j
jdo2
Asm
BCEL
JDBC driver (in this MySQL JConnector 5.1.6)

Wednesday, 24 September 2008 Department of Computer Science


Setting up the environment

 Create a new Java project


 Add to the project the libraries mentioned in the previous slide
 Create a new file called jpox.properties in the the source
folder of the project
 This file specifies different options like:
- the jdbc driver used for the connection
- the connection URL, username, password
- and others

Wednesday, 24 September 2008 Department of Computer Science


jpox.properties
javax.jdo.PersistenceManagerFactoryClass=org.jpox.jdo.JDOPersistenceManagerFactory

javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver
javax.jdo.option.ConnectionURL=jdbc:mysql://localhost:3306/demoshop
javax.jdo.option.ConnectionUserName=root
javax.jdo.option.ConnectionPassword=

org.jpox.implementationCreatorName = asm

org.jpox.autoCreateSchema=true
org.jpox.validateTables=false
org.jpox.validateConstraints=false

Wednesday, 24 September 2008 Department of Computer Science


Creating the classes

 Create the Classes used in the project


 In our case we create the Product and Book classes

 Add JPOX support to the project


 Right click the project folder go to JPOX->Add JPOX Support
 Right click the project folder go to JPOX->Enable Auto
Enhancement
 If you don’t use annotations:
- Right click the package go to JPOX ->Create JDO 2.0 Metadatafile

Wednesday, 24 September 2008 Department of Computer Science


Persistence Types

 Persistence Capable classes


 can be persisted to a datastore.
 they are core to JDO.
 need to be enhanced according to a JDO Meta-Data specification

 Persistence Aware classes


 manipulate Persistence Capable instances through direct attribute manipulation.
 enhanced with very minimal JDO Meta-Data.
 suffer very little changes to these classes.

 Normal classes
 are not persistable,
 have no knowledge of persistence.
 are totally unchanged in JDO, and require no JDO Meta-Data whatsoever.

Wednesday, 24 September 2008 Department of Computer Science


package.jdo
<jdo>
<package name="test">
<class name="Book">
<inheritance strategy="new_table" />
<field name="author“ persistence-modifier="persistent"/>
<field name="isbn“ persistence-modifier="persistent"/>
<field name="publisher“ persistence-modifier="persistent"/>
</class>
<class name="Product">
<inheritance strategy="new_table" />
<field name="name“ persistence-modifier="persistent"/>
<field name="description“ persistence-modifier="persistent"/>
<field name="price“ persistence-modifier="persistent"/>
</class>
</package>
</jdo>

Wednesday, 24 September 2008 Department of Computer Science


With annotations

@PersistenceCapable
@Inheritance(strategy=InheritanceStrategy.NEW_TABLE)
public class Book extends Product{
@Persistent
protected String author = null;
@Persistent
protected String isbn = null;
@Persistent
protected String publisher = null;

Wednesday, 24 September 2008 Department of Computer Science


Managing persisted Objects

 Create the Manager class


(this class will manage the persisted objects eg. create, select,
delete, update objects… )

 Operations on persisted objects are done within a transaction


tx.begin();
(Operations with objects)

tx.commit();

Wednesday, 24 September 2008 Department of Computer Science


Persist objects
PersistenceManagerFactory pmf =
JDOHelper.getPersistenceManagerFactory("jpox.properties");
PersistenceManager pm = pmf.getPersistenceManager();

Transaction tx = pm.currentTransaction();
try{
tx.begin();

Product test = new Product(“Cd Player",”A portable CD player",150.5);

Book book = new Book(“Web Services", “the blue book on the shelf“ ,47.96,
“Gustavo Alonso", " 3540440089 ", " Springer ");

pm.makePersistent(test);
pm.makePersistent(book);

tx.commit
}

finally{
if(tx.isActive())
tx.rollback();
pm.close();
}

Wednesday, 24 September 2008 Department of Computer Science


Retrieve Persisted Objects

tx.begin();

Extent<Product> prod = pm.getExtent(test.Product.class);

Query query = pm.newQuery(prod, "price > 0.0");


query.setOrdering("price ascending");

Collection<Product> pColl = (Collection<Product>)query.execute();


Iterator<Product > pIterator = pColl.iterator();

while(pIterator.hasNext()){
Product p = pIterator.next();
System.out.println(p.name + "-" + p.description+ "-" + p.price);
}

tx.commit();

Wednesday, 24 September 2008 Department of Computer Science


Update / Delete Objects

tx.begin();

Extent<Product> prod = pm.getExtent(test.Product.class);

Query query = pm.newQuery(prod, "price > 0.0");


query.setOrdering("price ascending");

Collection<Product> pColl = (Collection<Product>)query.execute();


Iterator<Product > pIterator = pColl.iterator();

while(pIterator.hasNext()){
Product p = pIterator.next();
p.setPrice(25.0); // Change the value
}

tx.commit();

Wednesday, 24 September 2008 Department of Computer Science


Transactions

 From Wikipedia:
A transaction is a unit of interaction with a
database management system or similar system
that is treated in a coherent and reliable way
independent of other transactions.
 Important for concurrent interactions on the
same data

Wednesday, 24 September 2008 Department of Computer Science 27

Anda mungkin juga menyukai