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.