Cover by Brian Carper, Christophe Grand, Chas Emerick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Hibernate

If you are already using Java or another JVM language for RDBMS work, it’s likely that you’re using Hibernate (http://www.hibernate.org), easily the most popular Java object/relational mapping library. One of the advantages of Clojure is being able to use Java libraries and frameworks seamlessly, and Hibernate is no exception.

Hibernate has a very different philosophy from Clojure: it operates by creating objects, mutating them, and translating those mutations into database queries. However, Clojure is flexible enough to allow Hibernate to work with very little fuss.

Setup

Let’s set up Hibernate to let us create, access, and update the authors table from earlier in the chapter. First, we’ll need to add Hibernate as a project dependency:

[org.hibernate/hibernate-core "4.0.0.Final"]

Most usage of Hibernate from Clojure will likely involve using domain objects already built in Java.[373] Here’s a vanilla Java class that represents author name data, using a mix of Hibernate and JPA annotations to indicate that it represents a database entity and specify the usual autoincrementing behavior of the id field:

package com.clojurebook.hibernate; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Entity; import org.hibernate.annotations.GenericGenerator; @Entity public class Author { private Long id; private String firstName; private String lastName; public Author () {} public Author (String firstName, String lastName) { this.firstName = firstName; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required