240 likes | 401 Views
SIGILUM. UNIVERSITATIS. REDONENSIS. UNIVERSITE DE RENNES 1. The tools used by esup-helpdesk developers: Eclipse, Omondo EclipseUML plugin, Apache OJB JDO. Pascal Aubry & Alexandre Boisseau IFSIC – University of Rennes 1 – May 2004 http://perso.univ-rennes1.fr/pascal.aubry/presentations/jdo.
E N D
SIGILUM UNIVERSITATIS REDONENSIS UNIVERSITE DE RENNES 1 The tools used by esup-helpdesk developers:Eclipse,Omondo EclipseUML plugin,Apache OJB JDO Pascal Aubry & Alexandre Boisseau IFSIC – University of Rennes 1 – May 2004 http://perso.univ-rennes1.fr/pascal.aubry/presentations/jdo
A Java program Program.java (runtime logic) runtime components Program.class javac Legend: user-maintained data tools and programs generated data
A uPortal channel Channel.java (runtime logic) implementation IChannel.java runtime components Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs generated data
A uPortal channel accessing a database database databasemanagement Data.java (data access) Channel.java (runtime logic) implementation IChannel.java runtime components Data.class javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs generated data
Accessing the database through (OJB) JDO database databasemanagement databaseobject map Data.java (data access) Channel.java (runtime logic) implementation OJB JDO enhancer IChannel.java runtime components Data.class JDO runtime javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs generated data
Using Omondo EclipseUML plugin database UML model OmondoeclipseUML databasemanagement databaseobject map code generation & reverse engineering Data.java (data access) Channel.java (runtime logic) implementation OJB JDO enhancer IChannel.java runtime components Data.class JDO runtime javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs generated data
Using Omondo EclipseUML plugin database UML model OmondoeclipseUML databasemanagement databaseobject map code generation & reverse engineering Data.java (data access) Channel.java (runtime logic) implementation OJB JDO enhancer IChannel.java runtime components Data.class JDO runtime javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs generated data
database UML model OmondoeclipseUML databasemanagement database object mapping databaseobject map code generation & reverse engineering Data.java (data access) Channel.java (runtime logic) implementation OJB JDO enhancer IChannel.java runtime components Data.class JDO runtime javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs missing feature generated data
database UML model OmondoeclipseUML databasemanagement database object mapping databaseobject map code generation & reverse engineering Data.java (data access) Channel.java (runtime logic) implementation OJB JDO enhancer IChannel.java runtime components Data.class JDO runtime javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs missing feature generated data
database UML model OmondoeclipseUML databasemanagement database object mapping databaseobject map code generation & reverse engineering Data.java (data access) Channel.java (runtime logic) implementation OJB JDO enhancer IChannel.java runtime components Data.class JDO runtime javac databaseaccesses databaseaccessconfig Channel.class javac runtimedata XMLoutput Legend: uPortal user-maintained data tools and programs missing feature generated data
JDO principles • Database abstraction • Uses JDBC • JDBC abstracts the database manager • JDO abstracts the data:no SQL code is written at all! • Persistence management • Perform changes on objects • the changes are automatically reflected on the data (in the database)
JDO specifications • http://java.sun.com/products/jdo/index.jsp • http://www.theserverside.com/articles/article.tss?l=JDO • http://www.onjava.com/pub/a/onjava/excerpt/JDO_chap3/
JDO implementations • Commercial • Kodo JDO (SolarMetric/Versant) • the most feature complete, easy to use implementation. • JDO Genie • pretty feature complete, but bad reputation. • JCredo • Open-source • OJB (Apache, open source) • JDO support is added through a plugin. A native JDO implementation will come with JDO 2.0. • TJDO (dead), XORM (not followed) • Castor JDO • Last release in march 2004. Middle product • JOnAS (ObjectWeb) • JOnAS is a J2ee specification which include JDO implementation. • Hibernate JDO (Hibernate) • seems to be a good product • JBoss announced an open-source JDO implementation in JBoss. • http://www.theserverside.com/discussions/thread.tss?thread_id=21825
The CUser class public class CUser { private String id; private String displayName; public CUser(String id, String displayName) { this.id = id; this.displayName = displayName; } public String getId() { return id; } public String getDisplayName() { return displayName; } }
The CDoc class public class CDoc { private long id; private java.sql.Timestamp date; private CUser sender; private String senderId; public CDoc(String description, CUser sender) {// id is automatically set by OJB-JDO. this.date = new java.sql.Timestamp( new Date().getTime()); this.sender = sender; this.senderId = sender.getId(); } public long getId() { return id; } public Timestamp getDate() { return date; } public CUser getSender() { return sender; } }
CUser database mapping <class-descriptor class="CExample.CUser" table="user"> <field-descriptor name="id" column="id" jdbc-type="VARCHAR" primarykey="true" /> <field-descriptor name="displayName" column="displayName" jdbc-type="VARCHAR"/></class-descriptor>
CUser database mapping <field-descriptor name="id" column="id" jdbc-type="VARCHAR"/> • Table user : • id (primary-key) • displayname <field-descriptor name="displayName" column="displayname" jdbc-type="VARCHAR"/>
CDoc database mapping <class-descriptor class="CExample.CDoc" table="doc"> <field-descriptor name="id" column="id" jdbc-type="BIGINT" primarykey="true" autoincrement="true"/> <field-descriptor name="date" column="date“ jdbc-type="TIMESTAMP"/> <field-descriptor name="senderId" column="sender" jdbc-type="VARCHAR"/> <reference-descriptor name="sender" class-ref="CExample.CUser"> <foreignkey field-ref="senderId"/> </reference-descriptor> </class-descriptor>
CDoc database mapping field-descriptor name="date"column="date"jdbc-type="TIMESTAMP"/> • Table doc : • id (primary-key) • date • sender-id • Table user : • id (primary-key) • displayname <reference-descriptor name="sender" class-ref="CExample.CUser"><foreignkeyfield-ref="senderId"/>
Programming with OJB // Get an Persistence Manager Factory (PMF) OjbStorePMF f = new OjbStoryPMF();// Get a Persistence ManagerPersistenceManager manager = f.getPersistenceManager(); // Start a transaction manager.currentTransaction().begin(); //// Do things... // // Commit or rollback the transactionmanager.currentTransaction().commit();
Example: display all users try { manager.currentTransaction().begin(); Query query = manager.newQuery(CUser.class); Collection allUsers = (Collection) query.execute(); Iterator iter = allUsers.iterator(); while (iter.hasNext()) { u = (CUser) iter.next(); System.out.println("id=" + u.getId() + ", displayName=" + u.getDisplayName()); i++; } manager.currentTransaction().commit(); } catch (Throwable t) { t.printStackTrace(); } finally { manager.close();} }
Example: create a user CUser u = new CUser("mon-id", "NOM prenom"); Transaction tx = manager.currentTransaction(); tx.begin(); // Mark object as persistant manager.makePersistent(u); try{ // Commit the transaction, object mark persistent is stored. tx.commit(); } catch(OjbStoreFatalInternalException ex){ System.out.println("User already exists!"); } finally { manager.close(); }
Example: delete a user Transaction tx = manager.currentTransaction(); tx.begin(); Query query = manager.newQuery(CUser.class, "displayName == \"Toto\""); Collection result = (Collection) query.execute(); if (result.size() == 0) { System.out.println("No user found!"); } else if (result.size() > 1) { System.out.println("Several users match!"); } else { CUser u = (CUser) result.iterator().next(); manager.deletePersistent(u); } tx.commit();