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.

About Ken Kousen
I teach software development training courses. I specialize in all areas of Java and XML, from EJB3 to web services to open source projects like Spring, Hibernate, Groovy, and Grails. Find me on Google+ I am the author of "Making Java Groovy", a Java / Groovy integration book published by Manning in the Fall of 2013, and "Gradle Recipes for Android", published by O'Reilly in 2015.

4 Responses to Hibernate Challenges

  1. Hey that helped….was googling for solution and bumped up on your page. Thanks.

  2. kousenit says:

    I’m very glad to hear that! That’s the biggest reason I have this blog in the first place — helping others overcome the obstacles I’ve encountered.

    Good luck!

  3. Pingback: ericlefevre » Blog Archive » Hibernate: so, the identifier mandatory *is* mandatory after all!

  4. Nahla Ghoneim says:

    Real thanks! I had a composite id that wouldn’t work except when I added the “optional” name attribute.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: