Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Build an object database

Construct a frontend to translate between Java objects and relational database records

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
One of the great strengths of Java is its ability to automate tedious programming tasks. For example, in the realm of I/O, object serialization automates the encoding of an arbitrary object as a stream of bytes. It could be done manually, but serialization makes it much more accessible and effective. Similarly, in the realm of networking, RMI (Remote Method Invocation) automates network requests between objects. That, again, could be done manually, but RMI makes it more accessible and effective. Finally, in the world of XML, work is under way to automate the generation of Java maps of XML document types, which will provide automated facilities for encoding and decoding XML documents. Again, much better than doing it manually.

TEXTBOX:

TEXTBOX_HEAD: Build an object database: Read the whole series!

:END_TEXTBOX

One notable gap in this capability is the realm of databases. In this article, we will present our first go at overcoming this limitation by providing automated transformations between Java objects and records in a database.

(Some credit for this article must be extended to JavaWorld reader Alasdair Gilmour, who graciously pointed us in the direction of accessing private object fields.)

Note: The article's full source code can be downloaded from Resources.

Terminology

Let's first get some terminology down. You see, I'm a firm believer in flat files, binary editors, and linear searches. There's really nothing you can't do with them. Since I'm now venturing into the scary world of databases, I want to make sure you know what I mean by ftang when I say ftang.

A database is a collection of tables. A table is a collection of records. A record is a collection of fields. Fields are the basic units of information in a database.

So, a database is a big bad backend system containing all of your information. Remember, information is power. A table is a specific subset of information of a particular type: for example, a table of all the employees in your company. A record is a single row within that table: for example, all information about a particular employee. Finally, a field is a particular datum within that record: for example, an employee's expendability rating.

In this article, we want to map between database records and Java objects. That means we want to be able to automatically translate between a Java User object and a particular record within, for example, an employees table in a database. Consequently, the name variable of the Java object will be mapped to the name field of the database record, and vice versa.

To accomplish this, we could simply serialize the Java object and throw the resulting binary blob into the database. But that's no fun. It does not lend itself to convenient access from anything but a Java program. In particular, we lose interoperability and we lose human accessibility. So we're not going there.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

a simple solution By lucio on December 7, 2008, 7:22 amhave a look on http://joafip.sourceforge.net/database/dbvsjoafip.html, this is a simple solution: write all in object relational fashion, then persist.

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources