I played with Hibernate today. I figured it was about time since I'd first read about it. It's basically a persistence layer offering powerful O/R mapping possibilities. And it's fully OO which I love. I don't know about you but I hate using Java and not being able to do extends and mock objects and other intersting things. Anyway, here's how I did it
I had a database with a couple thousand entries in 2 tables which I use for the development of I site a maintain (check it out...it's called eurobilltracker. I created it about a year ago when I wanted to do a little more SQL than select * from account where pk_id=1). Anyway, it's a table of users and a table of bills. Each user can have a referer (so this is a many-to-one I guess although not parent/child) and each line in the bill table is in fact a bill entry (if you don't know what I'm talking about, check out the site). Each user can enter bills in the system so there is a one-to-many relationship from a user to a bill and it needs to be bi-directional (not many-to-many... remember, each line in the bill table is a bill entry, not just a bill). I really should have a third table but I had decided to denormalize the bills and each entry each bill (the rationale being that few bills will be entered more than twice, that I am using mysql -ie no transaction support- and that I want each entry to be as quick as possible).
I'd say it took me about 2 hours to get the following to work
display all the information related to user with id=1 including all bills this user had entered
display a list of countries and the number of bills entered in these countries order by descending count
including the time to get the zip and read how to create the O/R mapping. I think that's pretty amazing. I can't imagine what it would have taken me from scratch with an EJB when I first learned about them... Here is a very interesting link for hibernate which lists all possible collections, maps, subclassing and other idiomatic configurations for the hibernate xml files.
I'm going to keep on testing it. I have started using JUnitPerf to see what happens what I query for an object, change it's value and flush it; the catch being that two threads are doing it at a time. Unfortunatly, right now, I'm getting various exceptions : SQL link exceptions, iterator exceptions... it's weird because I'm not sharing the Session object but oh well... I'll figure it out.
I can tell I've started being brain-washed by EJBs even though I have not used them that much. Questions that spring to mind about hibernate
how does it scale with multiple web servers
what happens when one "live" object is updated in one server ? Does a second server see it ?
how well are transactions handled ?
don"t I need object distribution ?
Pretty bad isn't it ?
Next time I'll write about DbUnit and how nice it was for us !
Posted by pgirolami76 at February 9, 2003 07:00 PM | TrackBack