training - day 3

30
Training - Day 3 OJB

Upload: yair

Post on 03-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Training - Day 3. OJB. OR Mapping is the mapping of relational database tables to objects (Java Objects in our case) Many OR Mapping tools available Hibernate iBatis OJB UIS supports Apache OJB Current version: 1.0.4. What is OR Mapping?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Training - Day 3

Training - Day 3

OJB

Page 2: Training - Day 3

What is OR Mapping?

• OR Mapping is the mapping of relational database tables to objects (Java Objects in our case)

• Many OR Mapping tools available– Hibernate– iBatis– OJB

• UIS supports Apache OJB– Current version: 1.0.4

Page 3: Training - Day 3

Apache OJB

• Open source project sponsored by the Apache Project

• Makes working with a database feel like working with an Java object model

• SQL is seldom used – OJB generates the SQL– Criteria objects

Page 4: Training - Day 3

Some of the features…• Store and retrieve Java objects to/from any JDBC compliant RDMS• No special interfaces needed for your OJB objects – transparent

persistence• Object graphs can be persisted by persisting the root object• Mapping support for 1:1, 1:n, and m:n associations• OR Mappings defined in an XML repository• Can use multiple databases• Lazy loading via proxies (JDK or CGLIB)• Support for polymorphism and extents• Multiple sequence manager implementations• Connection pooling and reuse of prepared statements• JTA and JCA integration• Optimistic (via number or timestamp) and pessimistic locking supported• Support for object caching

Page 5: Training - Day 3

Supported APIs• Persistence Broker (PB)

– Low level kernel API– Foundation of the others below

• ODMG– Object Data Management Group Persistence 3.0– Full support

• JDO– Java Data Objects– Partial support

• OTM– Object Transaction Manager– API or tool to build other tools on– Specific to OJB

Page 6: Training - Day 3

Persistence Broker

• Kuali supported API

• Low level API compared to OTM, ODMG or JDO, but still very easy to use

• Store, update, delete, and retrieve objects or collections of objects

• More on the PB after we learn how to configure OJB and map relationships

Page 7: Training - Day 3

OJB.properties

• Usually found on the root of the project classpath

• When upgrading OJB from any version, this file should be replaced and updated

• Key points– Location of the repository.xml file– How to obtain database connections– How to obtain proxies– How to manage transactions

Page 8: Training - Day 3

Example

• Let’s take a look at a real OJB.properties file

Page 9: Training - Day 3

The Repository Mapping

• repository.xml usually on the root of the project classpath

• A DTD is also needed

• Good idea to break up the contents of repository.xml for easy reuse (testing)– repository_user.xml : OR Mappings– repository_database.xml : Database config

Page 10: Training - Day 3

repository.xml<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE descriptor-repository PUBLIC "-//Apache Software Foundation//DTD OJB Repository//EN" "repository.dtd"[

<!ENTITY database SYSTEM "repository_database.xml"><!ENTITY user SYSTEM "repository_user.xml">]>

<descriptor-repository version="1.0" isolation-level="read-uncommitted" proxy-prefetching-limit="50">

<!-- include all used database connections --> &database;

<!-- include user defined mappings here --> &user;

</descriptor-repository>

Page 11: Training - Day 3

repository_database.xml<!-- Datasource example -->

<jdbc-connection-descriptor jcd-alias="MYDB" default-connection="true" platform="Oracle9i" jdbc-level="3.0" batch-mode="false" useAutoCommit="1" ignoreAutoCommitExceptions="false" testOnBorrow="true" testWhileIdle="true" testOnReturn="true" validationQuery="select 1 from dual">

<object-cache class="org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl"> </object-cache>

<sequence-manager className="org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl">

</sequence-manager>

</jdbc-connection-descriptor>

Page 12: Training - Day 3

<class-descriptor>

• Where you define your table to object mapping

<class-descriptor

class="edu.iu.uis.train.domain.User"

table="TRN_USR_T">

Page 13: Training - Day 3

<field-descriptor>

• A database column, or object property

<field-descriptor

name=”username"

column=”usr_nm"

jdbc-type="VARCHAR" />

Page 14: Training - Day 3

A few special fields• Primary keys

– primarykey=“true”• Auto-incrementing PK

– autoincrement=“true”– Will use the Sequence Manager that you have configured– If using Oracle sequences, you can set the sequence name: sequence-name=“some seq”

• Locking field (we use optimistic locking – fast, data integrity only checked on an update)– locking=“true”– Need to store a hidden locking field on JSP pages– Long – incremented each time the column is updated

• Long is recommended because of Oracle• Other possibilities are Integer and Timestamp

Page 15: Training - Day 3

JDBC TypesJDBC Type Java Type

CHAR StringVARCHAR StringLONGVARCHAR StringNUMERIC java.math.BigDecimalDECIMAL java.math.BigDecimalBIT boolean / BooleanBOOLEAN boolean / BooleanTINYINT Byte / byteSMALLINT Short / shortINTEGER Integer / intBIGINT Long / longREAL Float / floatFLOAT Double / doubleDOUBLE Double / doubleBINARY byte[]VARBINARY byte[]LONGVARBINARY byte[]DATE java.sql.DateTIME java.sql.TimeTIMESTAMP java.sql.TimestampCLOB ClobBLOB BlobARRAY ArrayDISTINCT mapping of underlying typeSTRUCT StructREF RefDATALINK java.net.URLJAVA_OBJECT underlying Java class

Page 16: Training - Day 3

<reference-descriptor>

• 1:1 Relationship

<reference-descriptor name=”movie" class-ref="edu.iu.uis.train.domain.Movie" auto-retrieve="true" auto-update="false" auto-delete="false" > <foreignkey field-ref=”movieId" target-field-ref="movieId"/></reference-descriptor>

Page 17: Training - Day 3

<collection-descriptor>

• 1:n Relationship or m:n Relationship

<collection-descriptor name=”userCollection" element-class-ref="edu.iu.uis.train.domain.User" auto-retrieve="true" auto-update="true" auto-delete="true" proxy="false"> <inverse-foreignkey field-ref=”userId" target-field-ref=”userId"/></collection-descriptor>

Page 18: Training - Day 3

repository_user.xml

• Let’s take a looks at this file in the exercises project

Page 19: Training - Day 3

Exercise: Time to map your business objects

• Open repository_user.xml and complete the TODO items– Need DDL for columns and types– Need domain objects for attribute names

(which are mapped to the columns)

Page 20: Training - Day 3

Getting back to the PB API

• You can use the PB to:– Query– Insert and update– Delete

• It can also tap into database transactions, which you should probably do just about all, if not all, the time

Page 21: Training - Day 3

Saving an objectpublic static void storeProduct(Product product) { PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); broker.beginTransaction(); broker.store(product); broker.commitTransaction(); }catch(PersistenceBrokerException e) { if(broker != null) broker.abortTransaction(); // do more exception handling }finally { if (broker != null) broker.close(); }}

Page 22: Training - Day 3

Saving multiple objectspublic static void storeProducts(List products) { PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); broker.beginTransaction(); for (Object productObj : products) { Project product = (Product) productObj; broker.store(product); } broker.commitTransaction(); } catch(PersistenceBrokerException e) { if(broker != null) broker.abortTransaction(); // do more exception handling } finally { if (broker != null) broker.close(); }}

Page 23: Training - Day 3

Deleting an object

public static void storeProduct(Product product) { PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); broker.beginTransaction(); broker.delete(product); broker.commitTransaction(); }catch(PersistenceBrokerException e){ if(broker != null) broker.abortTransaction(); // do more exception handling }finally { if (broker != null) broker.close(); }}

Page 24: Training - Day 3

Querying in OJB

• You can build up a template object and query • You can use raw sql if you need to bypass OJB• You can use a Criteria object

– Recommended way– This is the only way I will demonstrate for now, but there’s

great docs on the OJB website if you want to try something else

Page 25: Training - Day 3

A simple querypublic static Collection getExpensiveLowStockProducts() { PersistenceBroker broker = null; Collection results = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker();

Criteria criteria = new Criteria(); criteria.addLessOrEqualThan("stock", new Integer(20)); criteria.addGreaterOrEqualThan("price", new Double(100000.0));

QueryByCriteria query = new QueryByCriteria(Product.class, criteria); results = broker.getCollectionByQuery(query); } finally { if (broker != null) broker.close(); } return results;}

Page 26: Training - Day 3

More about the Criteria

• You can add just about any kind of “criteria” to build up an SQL statement

• Let’s take a look at the API

http://db.apache.org/ojb/api/org/apache/ojb/broker/query/Criteria.html

Page 27: Training - Day 3

Updating an objectpublic static boolean sellOneProduct(Integer stock) { PersistenceBroker broker = null; boolean isSold = false; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker();

Criteria criteria = new Criteria(); criteria.addLessOrEqualThan("stock", stock); criteria.addGreaterOrEqualThan("price", new Double(100000.0));

QueryByCriteria query = new QueryByCriteria(Product.class, criteria);

Product result = (Product) broker.getObjectByQuery(query);

if (result != null) { broker.beginTransaction(); result.setStock(new Integer(result.getStock().intValue() - 1)); broker.store(result); broker.commitTransaction(); isSold = true; } } catch(PersistenceBrokerException e) { if(broker != null) broker.abortTransaction(); // do more exception handling } finally { if (broker != null) broker.close(); } return isSold;}

Page 28: Training - Day 3

Exercise: The MovieDAOImpl

• It’s time to write your first DAO

• You shouldn’t need to extend any files, just implement the MovieDAO interface – Use eclipse to do this when you create the

class and it will create all of the methods for you

– Let’s do this part together

Page 29: Training - Day 3

A better way to do all this

• Now that you’ve seen how the PB API works, there’s a much better way to work with OJB

• We will see more of this when we talk about Spring

Page 30: Training - Day 3

References

• Object Relational Bridge– http://db.apache.org/ojb/

• OJB Tutorials– http://db.apache.org/ojb/docu/tutorials/sum

mary.html