I am about to show a tutorial which uses the in-memory hsqldb database via hibernate/jpa. First off, let's define the domain model. In this case, a scala class annotated according to hibernate style, about my buddies.
Note how the scala class, is much more compact than the java class, since we don't need the typical getter/setter boilerplate code. Now let's make sure, that the jpa modules and the database model is loaded. According to the hibernate specification, let's add the well known hibernate configuration file: resources/META-INF/persistence.xml:
After defining the persistency configuration, let's move on the main scala file:
The code is quite straightforward. Once the JPA EntityManager is created via the factory, the data model is available for insertion, deletion, query, using the methods defined in the hibernate and in the jpa documentation. In this example, I am not making use of injection frameworks such as spring or juice, but I am directly calling the factory from the hibernate package.
Well, that's that. The project has been set up using sbt. After retrieving the necessary packages, and compiling the source, running the tutorial will produce the following log:
HibernateJpaScalaTutorial:-:1.0.0> run
[info] Running nl.busa.jpa.HibernateJpaScalaTutorial
1 = Natalino Busa
2 = Angelina Jolie
3 = Kate Moss
[success] Total time: 4 s, completed Dec 9, 2012 4:18:00 PM
Just as a final note, a quick look at the sbt setup. This setup basically defines the repositories for resolving the packages, and the name and the version of the hibernate, and in-memory sql for the JPA persistency layer.
The setup is contained in a single sbt file, Build.sbt: