Java Ultra-Lite Persistence (JULP)
Tips and Tricks
- If you want to use TIMESTAMP or DATETIME or version column for
Optimistic concurrency locking you can use this trick:
JULP supposed to automatically retrieve PrimaryKey info from
DataBaseMetaData, and you can add your TIMESTAMP/DATETIME/version
column to PrimaryKey info and this column will be used for creating
WHERE clause for UPDATE and DELETE:
CREATE TABLE PRODUCT(ID INTEGER NOT NULL PRIMARY KEY,NAME
VARCHAR,PRICE DECIMAL, ..., TIMESTAMP DATE_MODIFIED)
...
String catalog;
String schema;
String table = "PRODUCT";
List pk;
...
pk = PKCache.getInstance().getPrimaryKey(catalog, schema, table);
pk.add("PRODUCT.DATE_MODIFIED");
PKCache.getInstance().setPrimaryKey(catalog, schema, table, pk);
...
factory.remove(product);
...
Generated SQL would be: DELETE FROM PRODUCT WHERE ID = ? AND DATE_MODIFIED = ?;
Note: retrieving PrimaryKey info from java.sql.DatabaseMetaData
occurred once
per JVM instance in DomainObjectFactory.load()
or
DomainObjectFactory.writeData()
, so if those methods never
called, the info is unavailable.
However you can call
PKCache.getInstance().getPrimaryKey(catalog, schema, table);
or
PKCache.getInstance().setPrimaryKey(catalog, schema, table);
- Normally to make your object able to persist you need to extend
org.julp.AbstractDomainObject
class. If you can't or don't want to extend, just make your class
implement org.julp.DomainObject
interface and copy and paste members
and methods from org.julp.AbstractDomainObject
into your class. Otherwise object will be enchanced in run-time
using CGLIB/ASM libraries.
-
To make field non-persistent just use
metaData.setReadOnly(metaData.getColumnIndexByFieldName(fieldName), true);