Using OJB with JDO in Cocoon


Note: Since Cocoon 2.1.3 OJBBlock is part of Cocoon.


This is a "howto" about using OJB-JDO in Cocoon.

In this Howto we will show how to set a simple example of using OJB-JDO with Cocoon. The example uses PostgreSQL database, but you can change it to use another RDMS.

Introduction

ObJectRelationalBridge (OJB) is an Object/Relational mapping tool that allows transparent persistence for Java Objects against relational databases.

OJB has 3 methods for access to Databases:

  1. PersistentBroker NewbieGuideToOJB

  2. ODMG
  3. JDO

Since JDO is the recommended successor of ODMG, we will try to show how to start using JDO with Cocoon.

Scenario

Environment

Solution

1. Download OJB 1.0.0 from http://db.apache.org/builds/ojb/ (approx 8MB)

2. Download the JDO libraries:

3. Create the auth_user table and the auth_user_seq sequence in your test database.

4. Create a file auth_user.jdo

5. Compile and enhance the auth_user.java bean.

Note: in auth_user.jdo, first change the DTD location to: <!DOCTYPE jdo SYSTEM "http://java.sun.com/dtd/jdo_1_0.dtd">

6. Create a file repository.xml in "TOMCAT_HOME/webapps/cocoon/WEB-INF/classes"

7. Copy files repository.dtd and OJB.properties in "TOMCAT_HOME/webapps/cocoon/WEB-INF/classes"

8. Copy the following files from the OJB distribution to "TOMCAT_HOME/webapps/cocoon/WEB-INF/lib":

  antlr.jar
  jdori.jar
  commons-dbcp.jar
  commons-pool.jar
  db-ojb-1.0.rc4.jar
  jdo.jar

9. Copy adduser.xml and sitemap.xmap into "TOMCAT_HOME/webapps/cocoon/ojbdemo"

Test

Open http://localhost:8080/cocoon/ojbdemo/adduser'''<<BR>> If all goes well, a record is added to your database.
If you press the refresh button in your browser you will get an error because you are trying to insert a new record with the same usr_login. (The column is UNIQUE).

Notes

The idea is that any request can "get" the global factory and ask his own PersistentManager. This is the correct way. After that we can use JDO as the "model" using java classes.

Readers comments.

Also, for new OJB people, you need to get j2ee.jar to build OJB (JD Daniels)

Attachment: Cocoon-OJB-JDO.zip

OJBWithJDO (last edited 2009-09-20 23:42:30 by localhost)