BANGALORE, INDIA: With most Java applications, developers have to deal with issues related to data persistence, mainly because of interfacing with a relational database. But as the application is based on an Object model and the database on a Relational model, an Object-Relational Impedance mismatch arises, with both systems taking a different view of how entities interact with each other. Objects have an implicit sense of identity denoted by references to memory location, while Relations have an explicit sense of identity denoted by the primary key of the table relationship attribute. This creates a gap between the operation of Objects and Relations. And it's for developers to bridge this gap.
Bridging the gap The easiest way to bridge this gap is through manual mapping, ie writing SQL queries using JDBC and retrieving results into variables. Manual mapping has always been a tedious task, and was made easier with automated object-relational-mapping tools or libraries like Hibernate or JDO. With these libraries or tools, objects are mapped to tables in relational DBMSes through the underlying framework that generates required SQLs for storing and retrieving object attributes. And even though these tools have minimized developers' tasks by auto-generating the necessary SQL queries, the mapping problems haven't gone away; in fact they've just moved over to configuration files (usually in XML format). When an application has a complex object model, it becomes more difficult to perform this mapping. Inheritance and many-to-many relationships are few such complexities that can't be directly represented in the relational model. Mapping of an inheritance model results in the creation of a set of tables which results in a conflict of weighing query performance versus the violation of normalization rules for the database. A solution for this would be to create a database that uses an object model for storage.
Direct Hit!
db4o: Object database The db4o (Database for Objects) is one of the few Open Source Object Oriented DBMSes that gives a new life to the concept of 'pure object storage' for object developers (based on Java or .NET platforms). With object databases, objects are stored as objects and thus avoid the impedance mismatch between applications and database models. With OODBMS, objects are stored in such a manner that they retain their nature throughout their lifetime; they are either in application or in a database domain. The object's content, structure and relationships are preserved, regardless of class complexities. OODBMS fits ideally into applications where the overhead of RDBMS is unnecessary, and a small-footprint database can suffice the need for the application. The benefits of using an object database like db4o include easier code maintenance and the ability to create new applications that have complex data models. db4o database engine allows storage of dynamic, free-form data that can be modified any time, unlike an RDBMS where tables are predefined and rigid. Thus, data persistence can easily be achieved using a db4o database engine. Due to this, db4o has found usage varying from PDA-based applications to flight navigation systems in aircrafts. Developers can directly work with persistent data without worrying about relationship mapping with the database. db4o is available for Java, .NET and Mono platforms. Another feature of db4o is that the database created by a Java application can be easily accessed using a .NET application and vice versa.
Getting started Download the latest 6.4 version of db4o from the link provided in the box. This version provides support up to JDK5. You just need to extract the contents of the archived file to the local disk before you can start using the db4odatabase. The directory db4o-6.4/lib contains few db4o-6.4xx-javaX.jar files, where javaX refers to the JDK version you want to use. In our case, it happens to be JDK5; so for installation we will use the db4o-6.4xx-java5.jar file. To install, we just need to add the path of the db4o-*.jar file to the system environment's CLASSPATH entry. If you are using a development environment such as Eclipse, you can add the db4o-*.jar file to the project's /lib/ folder. Since db4o runs in the same process space as the application, it gets called directly without employing any JDBC drivers.
Demo application Once the db4o-6.4-*.jar has been added to the system's classpath or to the application's projects /lib/ folder, we can start working on an application. To demonstrate the working for the db4o database engine, let's just create a Contact class having attributes such as name, number and email. The following code snippet shows the Contact class:
public class Contact { private String name; private String email; private String number; public Contact() { } public Contact(String name, String number, String email) {
this.name = name; this.number = number; this.email = email; } public String getName() { return name; } public void setName(String value) { name = value; } public String getNumber() { return number; } public void setNumber(String value) { number = value; } public String getEmail() { return email;} public void setEmail(String value) { email = value; } public String toString() { return "[Contact Info: " + "Name = " + name + " \t" + "Number = " + number + " \t" + "E-Mail = " + email + "]"; } }
To get an instance of the Contact class into the database for a relational model, you would need to create a schema describing the data type to the database and also have to map the columns and tables of the database with the fields of the Contact class. But in case of an object database, all this is not required; one just has to work with the object model of the application. The following code snippet shows how to generate the database based on the Contacts instance:
import com.db4o.*; public class Db4Demo { public static void main(String[] args) throws Exception { ObjectContainer db = null; try { db = Db4o.openFile("Contacts.yap"); Contact c1 = new Contact("Andrews", "9933211450", "andrew@gmail.com"); Contact c2 = new Contact("Kylie", "9350102010", "kylie@yahoo.com"); db.set(c1); db.set(c2); db.commit(); ObjectSet result = db.get(new Contact(null, null, null)); while (result.hasNext()) System.out.println(result.next()); }
finally { if (db != null) db.close(); } }}
In the above code snippet, db refers to an ObjectContainer that is used to open the new database. A db4o database is a single file with a .yap extension, and its set() method is used to store objects. In the above code, we are adding two object instances, c1 and c2, of the Contact class to the database in file Contacts.yap. To retrieve what is stored in the database, we use the get() method of the ObjectContainer; the result is retrieved as an ObjectSet. Moreover, in the code, if you look at:
ObjectSet result = db.get(new Contact(null, null, null));
You would notice that we are passing parameters as null, and so would be able to retrieve all entries from the database.
Similarly, if we want to update a record (for eg, change the email for a Contact named Andrews), the simplest way would be through the use of QBE (Query By Example), which is similar to the SELECT query of the SQL statement.
ObjectSet update = db.get(new Contact("Andrews", null, null)); Contact c = (Contact) update.next(); c.setEmail("andrew@aol.com"); db.set(c);
In the get() method we will pass the string Andrews for the name parameter and the matching result will be stored in the ObjectSet update. On calling Contact's setEmail() method, we can change or update the email entry in the database. db4o is easy to use for applications that require a low foot-print embedded database and where the object model of the application does not require a database that needs manual intervention (which is otherwise possible with relational databases like Oracle or MySql). More importantly, as the overhead on the application is reduced (since the developer doesn't have to code for relational mappings), db4o becomes an ideal choice for developers for small and embedded applications.
With Lotus Quickr your everyday business content can be easily shared, enabling operational efficiency. Download the Lotus Quickr demo to learn how.
know more..