Musings about Coding, Business and other Geek Stuff Live and Direct from somewhere on the planet
December 26, 2003
Reinventing the data abstraction layer (yet again)

For a long time I have been meaning to add somesort of data abstraction layer to the NeuClear Ledger which is the core book entry library used in NeuClear Pay.

What I wanted from this primarily is to:

  • Maintain and Create a Schema
  • Abstract away from the physical database
  • minimal configuration ( I hate XML as a configuration method)

I liked the looks of Hibernate and OfBiz’s Entity Engine, but everytime I wanted to start integrate one of them, it became a nightmare of dependencies and unwanted xml configuration files.

So I came to the conclusion that I dont mind writing sql, for my purposes it’s actually quite simple. So I simply want something now where I can maintain my schema with a minimum of fuss.

Which is why I have now added my own lightweight Entity Engine. It is currently in CVS as part of NeuClear Commons

My idea is to generate Entities using a simple programatic interface that suits my purposes.

The following is an example schema:

EntityModel ledgerModel=new EntityModel("ledger",true);
ledgerModel.addTitle();
ledgerModel.addTimeStamp();

EntityModel bookModel=new EntityModel("book",true);
bookModel.addTitle();
bookModel.addTimeStamp();

EntityModel xactModel=new EntityModel("transaction",true);
xactModel.addComment();
xactModel.addValueTime();
xactModel.addReference(ledgerModel);

EntityModel entryModel=new EntityModel("entry",false);
entryModel.addMoney();
entryModel.addReference(bookModel);
entryModel.addReference(xactModel);
entryModel.create(connection);

To create a table I simply instantiate a new EntityModel. The boolean field in the constructor allows me to pick one of two general primary key models that I use A large number or String URI. This model now contains the primary key and nothing more. I then simply add columns using the addXXX methods.

Several of these column methods define standard column names as well, such as id, comment, amount etc., to alleviate boring repetition.

Table relations are added by the addReference(org.neuclear.commons.sql.entities.EntityModel) method, which takes another EntityModel as a parameter.

Finally I just call the create(java.sql.Connection) method on one of the EntityModel’s which generates the table and any dependencies on the given JDBC Connection.

So the current status is development and unstable. I have the beginnings of an insert row method, which takes an object array. But to be honest, I dont see much point in that at the moment. I will do all inserts and selects manually for now.

Posted by pelleb at December 26, 2003 11:42 AM
This entry was posted in the following Categories: Java
Comments

If you want to generate mappings dynamically in hibernate then look at the mappings package/class....
but then again that would currently require a class object - and I don't know if you got a class for each of your entities ? (in hibernate 2.2 this restriction will probably be removed ;)

But otherwise i'm quite sure that existing bare-bones-sql creators exists on sourceforge...but i just can't remember the name of it right now ;(


Posted by: xam on December 26, 2003 03:16 PM

This looks like a version of what JGL tried to do with Java 1, which I personally found produced too many objects with too much code for any other than very simple queries. I understand why you would want to abstract the sql however in all but the very simplest of models I think this will get very complicated very quickly.

Posted by: Geoff on January 6, 2004 07:31 AM

First to Max. I would like to use Hibernate, but I would have to recreate my whole business object hierarchy, as I seem to remember it being relatively incompatible with it. (I cant remember the specifics though).

Geoff, I dont want to recreate JGL or anything like that. This is essentially for maintaining the schema across database engines. I find I keep writing the same crap, again and again.

I agree that straight SQL is the easiest for most applications.

Posted by: Pelle on January 6, 2004 10:00 AM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?