Scala + Hibernate overview

I've gotten a couple of questions about Hibernate and Scala. My last posts weren't really intended as a "gentle introduction" or anything: my goal was to document a difficulty I had with a particular situation. So here's more of an overview.

Basically, I didn't have to do anything special... I started out using JPA annotations for data binding, which worked very well. JPA maps fields by default, I guess, so normal Scala vars were mapped as I expected. In other words, this is enough to get going (off the top of my head):

class SomeClass {
  @Id @Generated
  var id: Long = -1
  var name: String = _
However, I did run into one issue with annotations. For some features (many-to-many mappings, for instance), JPA relies on nested annotation values, which are not yet supported in Scala. (I have opened a change request for this feature.) So I did end up switching to XML mapping files, which some people prefer anyway. The only things there are:

  1. if you want to map Scala vars without accessor methods, you should use default-access="field" on each mapping, and
  2. you need to remember the rules for the translation of Scala -> Java class-names.

I also defined some convenience methods for acquiring a Session and so on, but none of that is probably worth writing about, since it's pretty basic Scala stuff, and I'm no Hibernate expert anyway, so my stuff is probably not worth emulating. I really dislike the idea of having a single global SessionFactory, though, so I did go to some lengths to get rid of that. The solution is documented in my other posts on the topic.

Best of luck, and please do let me know if you have other questions. I haven't done a whole lot with it, but what I have done works very well!