?? readme.html
字號:
<HTML>
<HEAD>
<TITLE>JORA: Java Object Relational Adapter</TITLE>
<UL>
<LI><A HREF = "#introduction">Introduction</A>
<LI><A HREF = "#how">How it works</A>
<LI><A HREF = "#select">Selecting objects from database</A>
<LI><A HREF = "#manip">Insert, update and delete commands/A>
<LI><A HREF = "#about">Release notes</A>
</UL>
<BODY>
<HR>
<H2><A NAME = "introduction">Introduction</A></H2>
JORA will simplify you development of database application, establishing
implicit mapping between relational database tables and Java classes.
JORA is using JDBC for database connection, so it can be used with any
database supporting JDBC or ODBC. JORA also allows you to use object-oriented
features, such as polymorphism and complex objects,
in your database applications.
<H2><A NAME = "how">How it works</A></H2>
Programmer should create <B>Table</B> object for each class, which
should be mapped to database table. Usually name of the class is the same as
the name of database table. But it is possible to map several classes
with different names to one database table.
Also names of class components should match names of
table columns (it is possible for class to contain not all fields from
the database table, so class can be considered as some kind of table view).
A component of mapped on table class should be either of one the types,
supported by JDBC (see table <A HREF="atypes">Atomic types</A>), or
object with such components, or object implementing
<TT>java.io.Serializable</TT> interface.
In second case, database table should contain all fields
from classes of object compoentns. If class component is object implementing
<TT>Serializable</TT> interface, then database table should
contain large binary column for storing contents of objects closure.
Consider the following Java classes:
<PRE>
public class Point {
public int x;
public int y;
public boolean belongs(int left, int top, int width, int height) {
return x >= left && x <= left + width &&
y >= top && y <= top + height)
}
public static String sqlBelongs(String name,
int left, int top, int width, int height)
{
return name + "_x BETWEEN " + left + " AND " + (left + width) +
" AND " + name + "_y BETWEEN " + top + " AND " + (top + height);
}
}
public class R2Object {
public int id;
public String name;
public Point location;
public static Table table = new Table("R2Object", null, session, "id");
}
public class Circle extends R2Object {
public int radius;
public static Table table = new Table("Circle");
}
public class Line extends R2Object {
public Point lineTo;
public static Table table = new Table("Line");
}
</PRE>
And database containing correspondent tables:
<PRE>
create table Circle(
id SMALLINT PRIMARY KEY,
name CHAR(10),
location_x INTEGER,
location_y INTEGER,
radius INTEGER
);
create table Line(
id SMALLINT PRIMARY KEY,
name CHAR(10),
location_x INTEGER,
location_y INTEGER,
lineTo_x INTEGER,
lineTo_y INTEGER
);
</PRE>
Now it is possible to extract all records from <TT>Circle</TT> table
using <TT>select</TT> method of the <TT>Table</TT> class:
<PRE>
Cursor c = Circle.table.select("order by name");
Detail d;
while ((d = (Detail)c.next()) != null) {
System.out.println("("+d.location.x+","+d.location.y+") - " + d.id);
}
</PRE>
Or perform polymorphic query and select records from all these tables
using <TT>selectAll</TT> method of the <TT>Table</TT> class:
<PRE>
Object selection[] =
R2Object.table.selectAll
("where " + Point.sqlBelongs("location", 0, 0, 100, 100)).toArray();
for (int i = 0; i < selection.length; i++) {
System.out.println(((R2Object)selection[i]).name);
}
</PRE>
If you have large number of tightly linked objects, it is often very
inconvenient and inefficient to store all these objects in tables of
relational database. JORA allows you to pack the closure of such objects
and store it as BLOB field in database. Lets look at the following example:
<PRE>
public class Detail implement java.io.Serialized {
public int id;
public String name;
public Detail[] connectedWith;
public Detail(int id, String name, int nLinks) {
this.id = id;
this.name = name;
this.connectedWith = new Detail[nLinks];
}
}
public class Assembly {
public String name;
public double weight;
public Detail details;
public static Table table = new Table("Assembly", session, "name");
}
</PRE>
Objects of <CODE>Assembly</CODE> class can be stored in the following
table:
<PRE>
create table Assembly(
name VARCHAR(255) PRIMARY KEY,
weight REAL,
details LONG RAW
);
</PRE>
Such object cluster representing an assembly can be stored and retrieved
from database without any explicit efforts of programmer:
<PRE>
Assembly asm = new Assembly();
asm.name = "Z-195";
asm.weight = 30.5;
Detail bolt = new Detail(105, "bolt", 1);
Detail screw = new Detail(106, "screw", 1);
asm.details = bolt;
bolt.connectedWith[0] = screw;
screw.connectedWith[0] = bolt;
Assembly.table.insert(asm);
asm.weight += 1.1;
Assembly.table.update(asm);
asm = (Assembly)Assembly.table.select("where name='Y-315'");
System.out.println(asm.details.name);
</PRE>
<H2><A NAME = "select">Selecting objects from database</A></H2>
JORA supports two kinds of object queries from database:
simple query, when object are extracted only from one database table,
and polymorphic query, when objects are also selected from all database
tables, which are derived from the specified table.
Inheritance relationship between tables is specified by correspondent
relationship between Java classes mapped to the table.
JORA also supports notion of <I>abstract</I> table, which is not present in
database and is used only for issuing polymorphic queries
(table <TT>R2Object</TT> from the example above).
Abstract tables are created by constructor with <TT>tableName</TT>
parameter equal to <TT>null</TT>.<P>
When <TT>Table</TT> object is constructed, you should pass to constructor
name of Java class, name of database table, session object and name of primary
key for this table. If table name is the same as class name without package
prefix, then it is possible to specify only the class name: JORA provides
special constructor for this case. Session object is used to encapsulate
connection with database server. It should be opened before first access to
the table. Primary key is used for update and remove operation, to locate
necessary record within table. Primary key should be atomic.
It is possible not to specify session and primary key parameters in derived
tables, they will be taken from base table.<P>
<I><B>Important!</I></B>
All non-atomic components of mapped on
database table classes should have constructor with no parameters
(default constructor) to make it possible for JORA to create instances
of this object when data is loaded from database. Otherwise
<TT>NoSuchMethodError</TT> error will be raised.<P>
<I><B>JDK 1.1 restriction</I></B>
Prior to version 1.2 of JDK, it is not possible to access through reflection
mechanism fields of non-public classes from other packages.
That is why all classes, mapped on database tables, should
be defined as <TT>public</TT> (within package) and their components also
should be <TT>public</TT>, otherwise JORA package will be not able
to store and extract component values (<TT>IllegalAccessError</TT> will
be raised). There is no such restriction with JDK 1.2.<P>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -