View Natalino Busa's profile on LinkedIn

Principal Data Scientist, Director for Data Science, AI, Big Data Technologies. O’Reilly author on distributed computing and machine learning.​

Natalino leads the definition, design and implementation of data-driven financial and telecom applications. He has previously served as Enterprise Data Architect at ING in the Netherlands, focusing on fraud prevention/detection, SoC, cybersecurity, customer experience, and core banking processes.

​Prior to that, he had worked as senior researcher at Philips Research Laboratories in the Netherlands, on the topics of system-on-a-chip architectures, distributed computing and compilers. All-round Technology Manager, Product Developer, and Innovator with 15+ years track record in research, development and management of distributed architectures, scalable services and data-driven applications.

Sunday, December 9, 2012

Scala and Hibernate

Scala and Java can be mixed up in the same projects. Is it possible to get the best of both worlds? In particular, is it possible to combine the JPA framework, and Hibernate with the elegance of immutable structures and functional programming as provided by scala? It turns out that combining the two is not too difficult.

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:

Popular Posts

Featured Post

The AI scene in the valley: A trip report

A few weeks back I was lucky enough to attend and present at the Global AI Summit in the bay area. This is my personal trip report about th...