What’s The Deal with ORM?
ORM: Object-Relational Mapping. Chances are, if you’ve been doing web development for a significant amount of time, this concept has occurred to you. It’s the mapping of a high-level object definition in your OOP language of choice (e.g. a class) to a relational data storage scheme (e.g. your favorite database). I have written several ORM schemes in my day; naturally they started out as pretty naïve, but I would like to think that I’ve pretty well nailed it now, at least for my own personal needs and preferences.
Object-oriented PHP5 has been my development environment of choice for a while, but I’m getting more into Java in recent weeks. I’ve been programming in Java for many, many years, but I’ve never done any real server-side stuff with it. As luck would have it, my work duties have provided me an opportunity to dig my hands into J2EE.
I have to say, it has come a long way since I last checked it out. And I think I actually like it. It seems like a really magical MVC framework could be made for Java – it’s a pretty superior language to many of the other options (I like C# more, but I don’t like how it’s tied to the .NET framework in one way or another in all its incarnations). So I set about looking for that framework. I didn’t really like any of the ones I saw; they added lots of (in my opinion) needless complexity. I like simplicity in a framework. Minimalism, even. (Punchline coming soon, I promise)
So I thought I might set about rolling my own. My most recent framework for PHP5 is pretty money (if I do say so myself) so I thought I might carry over some of the concepts I developed there. I started looking at prefab ORM libraries for Java and of course the first one I checked out was Hibernate. After browsing the documentation, I have to say: Dear lord! It’s a great idea, but come on! A configuration file for each data class? An entirely new and dedicated query language! Sounds like once you allow Hibernate into your application, you’re stuck! Now I see why someone got fed up and made ActiveRecord – which I’m also not too fond of.
I think maybe I’m far too picky and far too lazy, which is a bad combination. The ORM solution I wrote for my PHP5 framework caters pretty well to these qualities – I write my data class code with my desired inheritance structure, add some tags in the comments for the fields (this is only necessary because PHP5 lacks strong typing) and then the ORM code takes care of the rest – determines the inheritance structure, creates the data schema (or updates it if it exists), and then can allow me to store and retrieve objects using either simple methods or real SQL if I need it. Foreign key relationships are handled automatically (example: $myPerson->employer fetches, caches, and returns the employer specified by people.employer_id) and life is good. This is what I want for Java. Do I have to do it myself? (Because that last one was a lot of work
)