EJB3 and Hibernate

Tonight at the Denver JUG the main topic was EJB3. While the spec isn't official just yet, it's as good as and people have started using it. The hibernate annotations developers, for example. I've been using these annotations for a while now on a couple of projects but I haven't really thought of them in the context of what's coming with EJB3. One of things coming is dependency injection from the EJB container. When the speaker mentioned this, it made much more sense of the dust up a few months back when the hibernate team started scrubbing references to IoC containers such as Spring from their website. Why bother with some other IoC when your parent (JBoss) is going to offer one. In one sense, this makes a little sense. On the other hand, EJB3's dependency injection doesn't really work outside the scope of the EJB classes. So if you want it for something that's not an EJB of one form or another, you're screwed.

I applaud the direction that EJB3 is taking: borrowing from RoR's example of convention over configuration, e.g. Having used that a fair bit with my Hibernate beans, I can say it's definitely a relief. The need for XML with EJB3 is almost nothing. The only XML the presenter used tonight was for the .par file (yes, another Blah ARchive) and that was for the datasource definition. It was about 7 or 8 lines. Nothing odious. A developer can use some XML to override the annotations at deployment time, if desired, but from how it appears, the XML is just about gone with EJB3.

EJB3 has some nice features that aren't really available in Hibernate, as far as I can tell, like extended transactions across calls for things like multiple step wizards. With the simple little example he went through tonight, EJB3 looks to be very very light indeed. The whole time I kept thinking, "These EntityBeans look like Hibernate POJOs." Which begs the question, "Why migrate to EJB3 if you're already using hibernate?"

And that's a fair question. There's not a lot of immediate value at first glance for sure. On the other hand, management of things like the SessionBeans and the like might be worth it. Integrated life cycle management from the container might be worth it. Overall, I didn't see a whole lot that says "Let's migrate!" However, EJB3 apps look more and more like how I've traditionally done Hibernate applications with some of the plumbing done for me. The one possible lack I see is the dependency injection being limited to EJBs only and no apparent way to plug in alternate implementations of those dependencies for testing. Have two DI mechanisms in place could get very messy, indeed. Perhaps the next version of the J2EE spec can address that. I haven't seen or used the new stuff enough to say whether that lack is real or perceived. Perhaps using the optional XML allows you to plug implementations. I'll have to wait and see.

Overall, I liked what I saw. EJBs certainly look a lot lighter than when I first saw 1.0 of the spec. And since EntityBeans are basically Hibernate POJOs (at least in JBoss' case), I'm halfway there.