|
[Java]
Getting sleepy with Hibernate
[
geir
]
Working on a new project, I've been trying to figure out what to do for data access. I've been trying to decide between using bog-standard JDBC hell vs O/R tools like Hibernate, Castor, Cayanne, OJB, TopLink etc vs JDO implementation. I had a few important requirements :
I liked what I saw with Hibernate and OJB, didn't like Cayanne because it looked like I have to extend their object model for my data classes, and was worried that TopLink would be too expensive and Oracle would eventually pull support for other (non-Oracle) databases. I also took a gander at JDO - one thing that made OJB attractive was that I could supposedly use JDO later on. After deciding that I had finally hit analysis paralysis, I decided to just get going with Hibernate. I heard good things from people I trusted, and there seemed to be a lot of activity surrounding the project. They have been getting tons of exposure on TSS. The fact that the lead developer is now working for JBoss is worrysome, but I figure that shouldn' t stop me. The result : I am both manic and depressive. Manic because I was able to get it working fairly quickly. There are tons of tutorials out there, but all seem to be written with v1.x in mind, and the object model seems to have changed significantly in v2.x. No biggie - they gave me the flavor of what to do. The model they use is very nice - very clean and simple, and I was able to get basic mappings going fairly quickly. I was able to save and read object, so I was happy. It did a few bizarre things, such as failing silently when I asked to 'saveAndUpdate()' for a new, fresh object. The cause was because I had incorrectly defined what the 'unsaved_value' of a key was, and thus, when given the choice to save (i.e do an INSERT) or update (do an UPDATE), it decided that the key wasn't the 'unsaved value', and thus tried to do an update, and failed. This is scary, because I'd like to know if a data operation fails, even if clearly my fault. Depressive? Well, I like to use JNDI for configuration of things like this in a mind-numbingly simple way. I uses a little JNDI context that saves everything to the file system. I want to produce a 'SessionFactory', the thing that allows you to get configured sessions so you can do useful work, stuff it into JNDI, and then have my apps just pull it out and use it. Then I don't have to worry about ensuring that my app/container/whatever has the config code at startup - I can do it outside the runtime environment and all is well (and testable, etc). Turns out, I either don't understand something, or the JNDI support in Hibernate just won't work for me. What they do is serialize the SessionFactory, but expect that the Hibernate environment has already been configured identically when you pull that SessionFactory back out of JNDI. Instead of de-serializing back into a SessionFactory, they have custom deserialization that just figures out the name of the configuration, and looks into the hibernate config running in the JVM already to get the SessionFactory for you. This means that you have to do the config each time in the JVM before deserializing the factory. I don't see the benefit of this over just letting me ask for it by name... Alas. I'll either figure out what I'm doing wrong, figure out why it's impossible, or just fix it. Anyway, the upshot is that I like HIbernate, and will continue with it. Post a comment
|