EJB’s in RAD6

This week I’m back in Westborough, MA, teaching EJB’s in RAD6.  EJB’s have a rather nasty learning curve.  Here that’s made worse by the fact that the students only have the previous two training classes as experience.

(This is the same group I taught Intro Java in May and Servlets and JSP’s in June.  The companion group in Philadelphia I’ll see again next week.)

Now that I’ve been spending all this time with Hibernate, entity beans look downright primitive.  I understand that EJB 3.0 completely revises them and makes them very Hibernate-like, but I haven’t had a chance to really dig into that.  Not to mention that most companies I deal with use WebSphere as their app server, which doesn’t even understand J2SE 5, much less Java EE 5.  Some day that will change, I guess.  In the meantime I’ll use JBoss for experimentation.

Last week I downloaded Eclipse 3.2 and ran the update wizard to install all the Callisto plug-ins.  The product took forever to start up.  Then, when I tried to make a simple Hello World servlet, the whole thing crashed for reasons unknown.  I’d hate to think they’ve turned Eclipse into RAD, with all the attendant issues, but we’ll see about that, too.

Speaking of Hibernate, I wish I’d realized earlier how valuable the Hibernate in Action book is.  Since it dealt with Hibernate 2 and the field has moved to at least Hibernate 3, I was afraid the material would be too dated to be useful.  That, as they say, turned out not to be the case.  Many of the answers I need are in there.

Once again, I wish I had the Matrix capability of absorbing an entire book in seconds.  It would be worth the metal plug in the back of my head.  It would even be worth it that it would pretty much put me out of business.  I have about a dozen books I need to grok sooner rather than later, and that would make it all so easy. 🙂

Even though the second edition (now called Java Persistence with Hibernate) is on the way, it won’t be available until November.  I wish it was part of Manning’s early access program, because I could really use it now.  Instead, I just bought the HiA ebook, since I already have the hard copy.  Now I can carry it along wherever I go.

I feel like I’m getting much closer to really understanding Hibernate.  I still have a long way to go, though.

At least one Hibernate resolution

Okay, one of the issues I identified is my fault. I can use JavaBeans that don’t have id fields as long as I leave the “name” attribute out of the <id> element in the mapping document. The reason I was throwing an exception is that I was still using the same test cases from before, and one of them tried to retrieve an element by id. Duh.

Okay, at least that makes sense.

Hibernate Challenges

Hibernate is proving to be a bit more challenging than I originally anticipated. The toy problems seem to work just fine, but I have a real (if very small) database schema and a real (if very small) set of Java classes mapped to them already. Trying to insert Hibernate in between is providing a lot of issues.

  • According to the Hibernate documentation , the <id> declaration in the mapping document has an optional name attribute. The text says “if the name attribute is missing, it is assumed that the class has no identifier property.” The Location class I’m using doesn’t have an identifier property, so I left name out of <id>.

No such luck. My test threw a HibernateException anyway, with the informative message: “The class has no identifier property.” Well, duh. Somehow I thought it was going to work anyway, but no such luck.

  • Apache Derby is proving to be quite awkward. Will really likes Derby, but I don’t think it likes me. I got tired of wrestling with the embedded driver. That driver is great as far as it goes, but doesn’t allow me to view the database inside MyEclipse in between tests. Even if I close the connection in between, the driver doesn’t recognize that the database is available.
  • I did eventually get the networked driver to work, but that wasn’t simple, either. I downloaded the Eclipse plug-in, but the client jar file that came with the plug-in apparently didn’t have the org.apache.derby.jdbc.ClientDriver class in it. I had to download the full version of Derby (admittedly not very large, but I didn’t know I was going to have to do that) in order to get the proper jar files.
  • Then I made an error. I put in what I thought was the proper URL for the networked driver and started the network server. The server seemed to come up okay (though in the plug-in I had to click an OK button to get the modal dialog to go away), but the test program kept throwing a “No suitable driver” exception. It took quite some time to realize I’d dropped a backslash from my URL in the hibernate.cfg.xml file. Once I fixed that, I could use the driver again.
  • The really annoying problem, however, is that while I can read from the database now, I can’t write to it using Derby. I keep running into the problem of trying to set the primary key and it doesn’t like that. In the create SQL script, the id attribute is set to “generated always as identity”, which I thought was fine, but it’s causing me problems.
  • In the Java classes, at least one of them has a setter that does validation before setting the attribute. The Job class has attributes minimumSalary and maximumSalary. The setMinimumSalary() method tests to see that the supplied argument is less than or equal to the maximum. That sounds fine, but apparently Hibernate is calling setMinimumSalary() before it calls setMaximumSalary() for each row. The first is therefore throwing an exception. I managed to fix that by using the attribute access=”field” inside the <property> for minimumSalary.

So, I’m working on it. Later I’ll look back on this as a valuable exercise, since it’s much closer to the real way this would be used in industry rather than the standard problems. I’ll be happier when it’s all working, though.

%d bloggers like this: