«

»

Mapping databases to objects

Some years ago I worked on an application that made use of some pretty heavy database access. We were using the SQLite database, which is sort of a library that contains a relational database which can be stored locally on disk. As the name suggests, it can perform SQL queries. After some months of coding the database interface was over and over filled with SQL-statements which in some cases were put together from different SQL-statement-segments at runtime and other evil stuff.

At one point I asked myself:
What if we now would have to make some major changes to the database model for whatever reason?

The answer was pretty simple:
We would surely die. Slowly and painfully.

We would be working ourselves through every single SQL statement in the whole project and check if it fits the new database model.

Luckily, humans are able to learn from their mistakes, at least to a certain extent. So what we are using now in our browsergame to access our database is a so called persistence framework that lets you map SQL statements which you define in an xml file directly into objects. You won’t find a single handwritten SQL statement in the whole code. Once you have defined how an object is retrieved from the database (like I said, in an xml file), you will never have to mess around with SQL statements concerning that kind of object again, in whatever context the object might occur.

There are several more or less complex persistance frameworks out there, so you’ll have to decide depending on your project which one suits you best. The one we are using is called iBATIS and is available under the Apache License 2.0.

Getting into iBATIS seemed quite exhausting to me at first, but after a few hours of trial and error and some major support from Jens I got it all together and would not want to miss it in our project.

So persistance frameworks FTW!!!!111one

Popularity: 1% [?]

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">