Hibernate and Middlegen

I had a chance to play with Hibernate and Middlegen this afternoon for a few hours in preparation for doing a Hibernate workshop with some programmers at work. My preference has always been to design my database schema and then generate the ORM layer on-top of that. I guess my reasoning is that the database is the heart of any large application and in many cases will have a web-app running against it, a 3rd party reporting system, an automated emailing system, etc… so it feels unnatural to me to use my web-app codebase to generate the schema.

This article on Hibernate's website got me started with Middlegen. After mucking with my ant build.xml I had Middlegen and Hibernate's hbm2java generating my hibernate mapping files and java domain model. Not bad.

Next I created some DAO objects and respective JUnit tests to play with some Hibernate queries to fetch my domain objects. For simple stuff QBE (Query By Example) is by far my favorite since it in no way ties you to an ORM suite and is the most object oriented looking approach to simple querying (to me atleast). HQL is also simple (and of course familiar) for doing joins and other more involved queries. I have the least experience with QBC (Query By Criteria) so I tend to stick with HQL. I guess with QBC I find myself translating it to an SQL like syntax in my head anyhow so HQL just feels more natural.

Next I want to try adding Spring into the mix for IoC and to get the benefit of Spring's generic unchecked exception hierarchies so I can stop polluting my DAO's with all of those try/catch blocks that are only there to catch unrecoverable errors anyhow. My take is, if it's not a recoverable error than the exception should be unchecked. That way I can catch it if I want but otherwise it will go right up the call stack without me having to declare it everywhere.

This entry was posted in Java. Bookmark the permalink.