(ALin 2006-12-27 00:28)
1. 源代碼生成,即是實現相關的接口,自己編寫相關的方法,在點類似於BMP EJB。
2. 源代碼處理。
3. 字節碼增強(使用字節碼增強工具:byte-code enhancer)。
Ø package.jdo
Ø alin/package.jdo
Ø alin/scut/package.jdo
Ø alin/scut/jdo/package.jdo
Ø alin/scut/jdo/Author.jdo
lib
|
jdo.jar(或者叫jdo2-api.jar)
|
JDO2.0 AIPI
|
|
jpox.jar
|
JPOX1.1.4實現
|
jpox-enhancer.jar
|
JPOX的字節碼增強器
|
|
log4j.jar
|
Log4j
|
|
bcel.jar
(jboss-4.0.2/server/default/lib目錄下面有)
|
Byte Code Engineering Library
(BCEL),這是Apache Software
Foundation 的Jakarta 項目的一部分。
|
|
src
|
alin/demo/Author.java
|
|
|
alin/main/…….java
|
這個包下面的源文件是示例應用程序
|
|
alin/demo/Author.jdo
|
|
jpox.properties
|
注意數據庫URL後面部分的作用:
SelectMethod=cursor(後面會說明)
|
|
log4j.properties
|
|
|
bin
|
alin/demo/Author.class
|
|
|
alin/main/…….class
|
|
alin/demo/Author.jdo
|
這些文件都是直接從src目錄下面複製過來的。
|
|
jpox.properties
|
||
log4j.properties
|
代碼1: Author.java
private int books;
private String name;
public Author(String name, int books) {
this.name = name;
this.books = books;
protected Author() {
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public int getBooks() {
return books;
public void setBooks(int books) {
this.books = books;
<?xml version="1.0" encoding="UTF-8"?>
"-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN"
"http://java.sun.com/dtd/jdo_2_0.dtd">
<package name="alin.demo">
<class name="Author" identity-type="datastore">
<field name="books" persistence-modifier="persistent">
</field>
<field name="name" persistence-modifier="persistent">
<column length="50" jdbc-type="VARCHAR"/>
</field>
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} (%t) %-5p [%c] - %m%n
# Each category can be set to a "level", and to direct to an appender
# Commented out sub-categories since the above setting applies to all subcategories
log4j.category.com.mchange.v2.resourcepool=INFO, A1
java org.jpox.enhancer.JPOXEnhancer alin/jpox/package.jdo
JPOX Enhancer completed with success for 2 classes. Consult the log for full details
java org.jpox.SchemaTool -props jpox.properties -create alin/jpox/package.jdo
JPOX SchemaTool (version 1.1.4) : Creation of the schema
2006-11-28 19:29:52 org.jpox.util.JDK14Logger info
信息: PersistenceManagerFactory - Vendor: JPOX Version: 1.1.4
2006-11-28 19:29:52 org.jpox.util.JDK14Logger info
信息: PersistenceManagerFactory initialised for datastore URL=jdbc:microsoft:sql
server://localhost:1433;DatabaseName=JDO driver=com.microsoft.jdbc.sqlserver.SQL
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: ================ DatabaseAdapter ==================
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Adapter : org.jpox.store.rdbms.adapter.MSSQLServerAdapter
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Datastore : name="Microsoft SQL Server" version="Microsoft SQL Server 200
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Driver : name="SQLServer" version="2.2.0022" (major=2, minor=2)
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: ===================================================
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Initialising Catalog "JDO", Schema "" using "None" auto-start option
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Catalog "JDO", Schema "" initialised - managing 0 classes
2006-11-28 19:29:53 org.jpox.util.JDK14Logger warn
警告: No manager for annotations was found in the CLASSPATH so all annotations a
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Managing Persistence of Class : alin.jpox.Product [Table : PRODUCT, Inheri
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
信息: Managing Persistence of Class : alin.jpox.Book [Table : BOOK, InheritanceS
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
2006-11-28 19:29:53 org.jpox.util.JDK14Logger info
2006-11-28 19:29:54 org.jpox.util.JDK14Logger info
2006-11-28 19:29:54 org.jpox.util.JDK14Logger info
信息: Creating foreign key constraint : "BOOK_FK1" in catalog "" schema ""
2006-11-28 19:29:54 org.jpox.util.JDK14Logger info
2006-11-28 19:29:54 org.jpox.util.JDK14Logger info
信息: Validated 1 unique key(s) for table PRODUCT
2006-11-28 19:29:54 org.jpox.util.JDK14Logger info
2006-11-28 19:29:54 org.jpox.util.JDK14Logger info
// MakePersistent.java -- Create an Author object and persist it.
public class MakePersistent {
PersistenceManager pm = pmf.getPersistenceManager();
Author au = new Author("Eric Ben", 5);
System.out.println("Author: " + au.getName() + "/t" + au.getBooks()
// Can not read fields outside of transactions. Or set:
// System.out.println("Author: " + au.getName() + "/t" + au.getBooks()
System.out.println("Author: " + name);
// MakePersistent.java -- Create an Author object and persist it.
public static void main(String[] args) {
PersistenceManagerFactory pmf =
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Do other things.
public static void main(String[] args) {
PersistenceManagerFactory pmf = JDOHelper
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Query query = pm.newQuery(Author.class, "name == /"Eric Ben/"");
Collection result = (Collection)query.execute();
Author au = null;
if(result.iterator().hasNext()) {
au = (Author)(result.iterator().next());
// query.closeAll();
if(au == null) {
System.out.println("No Author found!");
// Can npt read fields from deleted objects. So this statement below
// will throws Runtime Exception: JDOUserException.
// System.out.println(au.getName());
// If we don't want update, we can use tx.rollback() instead of
// tx.commit()
String name = au.getName();
System.out.println("Author: " + name + " |/t" + au.getBooks());
Query query = pm.newQuery(Author.class, "name == /"Eric Ben/"");這一句創建了一個查詢,第二個參數是JDO QL語句,具體語法這裏不說了,較多。有的實現可以設置成使用SQL語句。
public static void main(String[] args) {
PersistenceManagerFactory pmf = JDOHelper
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Query query = pm.newQuery(Author.class, "name == /"WeiQin Sun/"");
Collection result = (Collection) query.execute();
Author au = null;
if (result.iterator().hasNext()) {
au = (Author) (result.iterator().next());
// query.closeAll();
if(au == null) {
System.out.println("No Author named [WeiQin Sun] found!");
au.setName("Jie Hou");
// If we don't want update, we can use tx.rollback() instead of
// tx.commit()
String name = au.getName();
System.out.println("Author: " + name + " |/t" + au.getBooks());
Ø 使用導航
Ø Extent查詢,Extent代表某個可持久類的所有實例(可以包括其子類)
Ø Query查詢,Query則代表滿足一些條件的實例,如叫某個特定名字
String name = author.getName(); // author是導航引用
// ReadExtent.java -- Read by Extent.
import java.util.Iterator;
public class ReadExtent {
PersistenceManager pm = pmf.getPersistenceManager();
Extent extent = pm.getExtent(Author.class, false);
System.out.println("Author: " + au.getName() + " |/t"
public static void main(String[] args) {
PersistenceManagerFactory pmf =
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Query query = pm.newQuery(Author.class, "books == 5");
Collection result = (Collection)query.execute();
Iterator itor = result.iterator();
Author au;
while(itor.hasNext()) {
au = (Author)itor.next();
System.out.println("Author: " + au.getName() + " |/t"
+ au.getBooks());
出現:Can't start a cloned connection while in manual transaction mode