340 likes | 351 Views
This article provides an overview of objects, their attributes, and persistence in Java, with a proposed intermediate solution for simple and complex persistence options. Includes code review and a demo.
E N D
A Simple Java Relational Database Thomas A. Bullinger March 20, 2001 tomb@emrt.com
Overview • Objects • Object Attributes • Persistence • Simple • Complex • Options in Java • Proposed Intermediate Solution • Code Review • Demo • Questions www.emrt.com
Properties of Objects • What is an object? • State • Data associated with the object • Behavior • Functions to operate on the data • Identity • The distinguishing characteristic of objects www.emrt.com
Object State • Can be classified in multiple ways • Stack-based (typically local, not object scope) • Heap-based (local or object scope) • Static • Object based (automatic) • Persistent www.emrt.com
Stack-based • Code Example … int countThings(Vector things) { int thingCount; … } … www.emrt.com
Heap-based • Code Example … Vector myCollection = new Vector(25); … www.emrt.com
Instance-Based • Code Example Root=C:\Project1 DataFilename=datasetOne.dat Class MyClass { public int thingCounter = 25; … } www.emrt.com
Class-Based • Code Example Class MyClass { public static int THING_COUNTER = 25; … } www.emrt.com
Persistent • Requirements for persistence • Should be transparent to client code • Should be type-safe • Should support object relationships • Inheritance • Composition • Should support dynamic queries www.emrt.com
Simple Persistence • java.util.Properties • A simple persistence mechanism • Based on Key/ Value pairs (one-to-one relationship) • Subclass of java.util.Hashtable • Data represented by Strings • Strings are polymorphic • Methods of interest in Properties: • Object setProperty(String key, String value) • String getProperty(String value) • void store(OutputStream out, String header) • void load(InputStrream input) • Enumeration propertyNames( ) www.emrt.com
PropertyFile • A simple subclass of java.util.Properties • Methods of interest in PropertyFile • Constructors • PropertyFile(String filename) • PropertyFile(PropertyFile props) • Persistence (overloaded methods) • store(String filename, String header) • store( ) • Accessors • String getFilename( ) • (plus all methods of java.util.Properties) • Source Code: PropertyFile.java www.emrt.com
PropertyFile Contents Contents of “Config.ini”: Root=C:\Project1 DataFilename=datasetOne.dat www.emrt.com
PropertyFile Example import PropertyFile; … PropertyFile myConfig = new PropertyFile(“Config.ini”); String rootDirectory = myConfig.getProperty(“Root”); String dataFilename = myConfig.getProperty(“DataFilename”); PropertyFile dataFile = new PropertyFile(rootDirectory + “\” + dataFilename); … www.emrt.com
Strings are Polymorphic int count = Integer.parseInt(myConfig.getProperty(“TotalFiles”); double grade = Double.toDouble(myConfig.getProperty(“AverageGrade”); String myName = myConfig.getProperty(“LastName”) + “, “ + myConfig.getProperty(“FirstName”) + myConfig.getProperty(“MiddleInitial”); www.emrt.com
One-to-Many Relationships • Properties map one key to one value • What if there are multiple values? • StringList is a collection of comma-separated strings • Implements Enumeration interface to walk the list • Methods of interest: • StringList(String list) // constructor • int length( ) • String toString( ) • boolean hasMoreElements( ) • Object nextElement( ) • Source Code: StringList.java www.emrt.com
StringList Usage String myNames =myConfig.getProperty(“NameList”); StringList myNameList = new StringList(myNames); while(myNameList.hasMoreElements() == true) { System.out.println(“Name: “ + myNameList.nextElement()); } www.emrt.com
What about Objects? • Serializable as a solution • Used for RMI, can be leveraged for persistence • Requires assorted support methods • writeObject • readObject • writeReplace • readResolve • Binary data representation • Does not support dynamic queries www.emrt.com
Complex Persistence • JDBC • Layered on a relational database • Specifies an interface only • Requires a database server • Oracle • Microsoft SQL Server • Microsoft Access • Others • Requires administration • Requires Database architecture • Impedance Mismatch (object / relational) • Suitable for large applications www.emrt.com
Intermediate Solution • Map an object onto a PropertyFile • PropertyFile contains data elements of object • Persistent Class provides methods and conversions • Persistence mechanism hooked into constructor / destructor • One subdirectory for each class • Subdirectory contains a PropertyFile for each instance • PropertyFile contains each attribute and value • Persistent base class provides persistence mechanism and attribute accessors www.emrt.com
Persistence Participants • PropertyFile • DataStore • Storable • PropertyFileCache • Application-Specific classes www.emrt.com
DataStore • Singleton factory for persistent objects • Based on directories of PropertyFiles • Provides support for dynamic queries • Provides support to serialize / deserialize attributes • Provides support for object deletion • Can be pointed to different sets of objects • Source Code: DataStore.java www.emrt.com
Storable • The base class for storable objects • Provides interface to DataStore for serialization / deserialization • Provides version capability • Provides generic data accessors (protected) • getValue • setValue www.emrt.com
Storable (continued) • Provides other accessors • isPersistent • isTransient • isDirty • getVersion • getType • isNew • Source Code: Storable.java www.emrt.com
PropertyFileCache • A dynamic collection of PropertyFiles • Keeps a file in memory for the next access • A performance optimization for queries • Can get you in serious trouble! www.emrt.com
MapFile • A mapping of instance name to Directory & PropertyFile • Example: mapfile.dat www.emrt.com
Storable Subclasses • Inherits from Storable • Provides hard-coded type & version • Provides accessors and conversions for each data element (if required) • Provides class-specific behavior • Example: Video.java, MovieStudio.java www.emrt.com
Relationships • Implemented via instance name references • StringList provides one-to-many relationships • Many-to-many implemented via intermediate classes (like any other RDB) • Implementing class required to perform lookups as needed • Example: Video.java www.emrt.com
Queries • Client provides a Properties instance for query • Query behavior examines each potential instance and matches candidates to contents of query set • Matches are returned as a Vector of “hits” • Client must traverse “hit list” www.emrt.com
Query Example • Source Code: RJugDemo.java www.emrt.com
Pros • Quick & Easy • Data represented as text in ASCII files • Support for simple queries • Limited versioning • Easy extensibility • Support for OO constructs • Composition (via explicit relational lookup) • Inheritance (via traditional mechanism) www.emrt.com
Cons • Slow (especially for complex queries) • Doesn’t scale well • Requires explicit accessors • Or client knowledge of type mapping • Requires mapping to/from Strings • Practical versioning may be problematic • Automatic updates? www.emrt.com
In Action! • Source Code: RJugDemo.java • Execution: • Database Test Tool: www.emrt.com
Other Issues: • Programmatic Creation • Transient Objects • Caching www.emrt.com
Questions? www.emrt.com