|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.object.DAO
DAO is the core of the Adept persistent storage mechanism. All database objects inherit from it. All fields that are not static, final or transient are saved. Objects as well as primative are written to any depth necessary. Strings, Maps, Collections and DAOs are treated specially. A DAO field inside a DAO will be saved as a pointer into the database of the inner field. This makes the objects fully relational. Care must be taken with DAO fields that they are retrieved from the database when the outer DAO is created. They are committed before saving the location. This will cause changes to be recorded on update() or add(). If they are not retrieved from the database nor set as update() or add(), they will not be saved and later retrieved as empty. As well as individual inner DAOs, Adept supports array, Collections and Maps that include DAOs in a relational manner.
static class MyDAO extends DAO
{
public int integer; // can contain any form of primative
public String string; // and Strings
public MyOtherClass myOtherClass; // and any other class you have defined to any depth
public HashMap nameValuePairs; // Maps are saved and NVP for more efficient storage
public DAO anotherDAO; // normalised - committed and saved as a database pointer only
public ArrayList myList; // as are Collections
public Date timeStamp; // a very special field - with the record timestamp of the last record update
public static class PrimaryIndex extends Index { int integer; String string; }
public static class SecondaryIndex extends Index( String string; }
}
DAO.associateDatabase("db1", false); // in init code - for all access ojects
// ... and for each DAO as needed ...
MyDAO myDAO = new MyDAO(); // will be in the last (or only) database used.
If the application needs to run multiple databases, the DAO objects need to
be associated with the correct database. See Database for examples.
Association is not mandatory. If it is not done, the class is associated with
the default database for the application. This database is named after the
program in the output directory. The index size can be be set directly from
properties.
When adding a new type of DAO to the system, the database needs a guess on
the likely number of records to size the index. This is not a critical
requirement. Only if it is 10 times too small will performance degrade
perceptibly. Too large a figure will cause some wasted disk space, but again
not excessive. This maximum can be specified on the associate() command as
above, in the properties file (as indexClassName.indexSize ) or use
the default of 1,000,000. To get the required property key, run once without
and get the full class name from the log file.
static class MyDAO extends DAO
{
...
public void update() { throw IOException( "Illegal commit()"); }
void myDAOupdate() { super.update(); }
}
MyDAO myDAO = new MyDAO(); myDAO.update(); loadMyDAO(myDAO, 1); myDAO.commit(); MyDAO dao = myDAO.copy(); dao.update(); changeMyDAO(dao, 2); dao.commitUnique();update() is mandatory for changing existing records and must be called before changing the data when updating a record.
MyDAO dao = search.first("test record", false);
if (dao == null) dao = defaultDAO.copy();
dao.update();
// ...do the normal stuff
changeMyDAO(dao);
dao.commit(); // will only write if the dao was actually changed
The example above will use update() for either depending on whether an
existing record is found.
Updates can be dropped instead of written to disk by using discard() instead
of commit().
The basic system allows duplicate entries. If you have an application with a
unique constraint you need to account for that in the code. The above example
does this by first searching for a record and only adding it if not found.
A commit is done explicitly when a DAO is closed or finalised.
The current record can be deleted, along with all indexes.
if ((dao = search.first("my key", false)) != null) dao.delete();
All DAO based classes implement comparable with super.comparable();
Class MyPojo
{
String data;
Class index extends Index { String data; }
}
MyPojo myPojo = new MyPojo();
DAO dao = DAO.getInstance( myPojo); // call whenever you need to retrieve DAO interface
myPojo = (MyPojo) dao.data(); // to retrieve data from DAO
If you prefer not to sully your data with index references you can create a new DAO type
with indexes and associate that way.
Class MyPojo
{ String data; }
Class MyPojoDAO
{ Class index extends Index { String data; } }
static { DAO.associate( MyPojo.class, MyPojoDAO.class); }
MyPojo myPojo = new MyPojo();
DAO dao = DAO.getInstance( myPojo); // call whenever you need to retrieve DAO interface
myPojo = (MyPojo) dao.data(); // to retrieve data from DAO
Index,
Database,
ObjectXML,
ObjectScraper| Constructor Summary | |
DAO()
DAO instantiation. |
|
| Method Summary | |
void |
close()
Close the DAO - writing if dirty. |
void |
commit()
Replace existing object with changes (if any are recorded). |
void |
commitImmediate()
Replace existing object with changes (if any are recorded). |
int |
compareTo(Object that)
So that DAO value objects can be sorted, added into trees, etc. |
DAO |
copy()
Return a copy of a DAO object that can then be added to the database as a new record. |
void |
delete()
Delete the current object from the database. |
void |
discard()
Discard any changes made to the current object. |
boolean |
equals(Object object)
For hashmaps a DAO is equal if it is the same record on disk. |
boolean |
find()
Search the database for a unique DAO. |
DAO |
get(int record)
Get the record from the cache if available, otherwise from the database. |
int |
getCurrentRecord()
External access to current record is via a getter so that if a new DAO has been created it is committed to disk to provide a current record for use elsewhere. |
int |
hashCode()
For hashmaps a DAO is equal if it is the same record on disk. |
boolean |
isActive()
If the DAO fails to initialise it cannot throw an exception. |
DAO |
load()
If you are using lazy loading on a first/next, then you will need to use load() on DAOs inside the DAO returned (relational DAOs) to ensure that the data is loaded. |
static DAO |
newInstance(Class daoClass)
Factory method to make a new instance of a DAO. |
void |
readOnly()
Sets the DAO being created to read/only. |
void |
setCurrentRecord(int record)
Set the current record when an initialiser just won't do. |
String |
toString()
The String representation of an object is in XML. |
void |
update()
Mark an item to save on commit if changed. |
| Methods inherited from class java.lang.Object |
getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public DAO()
| Method Detail |
public void readOnly()
public boolean isActive()
public void close()
close in interface CloseableCloseable.close()
public DAO get(int record)
throws IOException
record - database record to retrieve
IOException
public DAO load()
throws IOException
IOExceptionpublic DAO copy()
public void update()
public boolean find()
throws IOException
IOExceptionpublic void commit()
public int getCurrentRecord()
throws IOException
IOExceptionpublic void setCurrentRecord(int record)
record - to set as current record.
public void commitImmediate()
throws IOException
IOException
public void delete()
throws IOException
IOExceptionpublic void discard()
public boolean equals(Object object)
object - to compare against.
Object.equals(java.lang.Object)public int hashCode()
Object.hashCode()public int compareTo(Object that)
compareTo in interface Comparablethat - to compare to.
Comparable.compareTo(java.lang.Object)public String toString()
Object.toString()public static DAO newInstance(Class daoClass)
daoClass - Being the class for the DAO we want.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||