A Funny Java Flavoured Look at the World

Thursday, April 27, 2006

why I wanted to use Hibernate but couldn't

I have been recently working on some JDBC connection code. I was hoping to use hibernate for the connection stuff even though I had already written some code to connect to a database. The reason why I wanted to use hibernate was if the user then changed their datasource then it would be an easy case of just changing the connection config, which in hibernate is called hibernate.cfg.xml

One of the advantages of using hibernate is it has it's own sql language - hql. This means you can write some Hql and then it will convert this into any database you want to use. The beauty of this is you no longer have to worry about the different sql syntax on different databases.

I am currently trying to use Access the sql is just slightly different

this worked in oracle


Select Place_Name,easting,Northing from citytown where lower(Place_Name) like lower('a%') order by Place_Name where rownum < 21


where as this works in access

Select top 20 Place_Name,easting,Northing from citytown where (Place_Name) like ('a%') order by Place_Name

there are other examples of slight syntax differences which means you have create different sql statments depending on what connection you are using.

The other benefits of using Hibernate is they have more developers working on it, so you are getting a good solid product with many hours of effort and testing put it, not to mention any future development. Hibernate also supports lots of databases which you might need in the future. Not to mention their database connection code is firstly much better written, faster and more efficent.

Another reason why I am glad I have looked at the hibernate and got some oracle examples working was because lots of companies are using it at the moment, so it's a good thing to put on your CV.

I was also impressed with the design of hibernate with regards to the code intergrating with the configuration files. It made it very flexiable because the configuration files make it very modular, allowing you to plugin your config files and point to Java files you have created but without having to really extend any of the hibernate classes. As with many well designed pieces of software I am also impressed by how such good design can look so simple, a sort of complex simplicity. Hibernate was very easy to setup and use and comes with an excellent tutorial, which is straight forward and easy to follow.

I like the idea of mapping database tables to POJO's (plain old java objects) and abstracting the database to a connection file.

I'm certain Hibernate has many other qualites and this isn't a review of Hibernate, it was more a blog about why I wanted to use

Although I couldn't use Hibernate because hibernate doesn't support Microsofts Access database, well I it doesn't support it on their supported Databases page. I had a look and someone had done some work on it but I couldn't be bothered with trying to implement their own access dialect. I found it very useful and interesting trying hibernate and I look forward to using it in the future.

I view hibernate and using things like it, similar in some ways to using Struts in your web app. Let Struts/Hibernate do all the boring fundamental/structural code so you can concentrate on writing the more exciting crowd pleasing code. In Hibernates case I don't want to write code to deal with different connection urls or the different flavour of syntax used by different database, let hibernate deal with it. It also has the benefit of becoming an industry standard, so if a new developer joins your team, there is a decent chance he will understand hibernate where as he would probably have to learn to unravel some home made database connection code.

The final comment I would like to make is I would urge developers who have only used hibernate to write some connection code yourself, so at least you know what hibernate is doing and then you can appreciate why using hibernate is a good idea.

if anyone would like to comment on their experiences with hibernate please do, I would be interested into what other people think about it.

0 Comments:

Post a Comment

<< Home