MyEclipse + JPA + Spring + Hibernate = DAO (mostly)

I just about have my Google Maps mashup working. When it’s deployed, it’ll show markers for each location where I’ve taught a course. Eventually I’ll filter them by year, connect them to home with polylines, and maybe more.

As a mashup, it’s not the most interesting application ever written, but it’s given me a good opportunity to learn the Google Maps API and to play with some other interesting technologies.

In this case, that means Spring 2.0 and the Java Persistence API (JPA). I needed some way to get my course data into and out of a database, and I certainly didn’t want to hardwire anything if I could avoid it. That meant I really needed a Data Access Object (DAO) layer for my courses.

I’ll describe the details over the next few posts, but here’s the good part. The newest version of MyEclipse, version 6.0, makes it extremely easy to add both Spring 2.0 libraries and JPA to a given project. The wizard provided asks for which JPA provider to use, with the available options being TopLink and Hibernate. Just because I’m familiar with it, I chose Hibernate.

(That lead to a serious bug later involving conflicts in the asm jar file, which I’ll also describe later.)

I also used Java 5 annotations to do the mappings, as in:


@Entity
@Table(name="locations")
public class Location {
    @Id  @GeneratedValue
    private int id;

    private String city;
    private String state;
    private double latitude;
    private double longitude;

    // ... etc ...
}

I also used Spring’s @Transactional attribute in order to build a service layer that interacted with the CourseDAO. That was seriously easy, especially when I had the excellent Spring in Action book to use for reference. Since I already planned to use Spring’s dependency injection to manage the DAO classes into the service layer (i.e., I had Spring inject the transaction manager, the data source, the entity manager factory, and whatever annotation capabilities I needed), I let Spring manage as much as possible.

Just for completeness, I wrote my DAO tests in Groovy, too. 🙂

The process was all kinds of fun, actually. As soon as I decide the code is good enough, I’ll describe all the pieces here. In the meantime, I’m just rather insufferably pleased with myself.

About Ken Kousen
I teach software development training courses. I specialize in all areas of Java and XML, from EJB3 to web services to open source projects like Spring, Hibernate, Groovy, and Grails. Find me on Google+ I am the author of "Making Java Groovy", a Java / Groovy integration book published by Manning in the Fall of 2013, and "Gradle Recipes for Android", published by O'Reilly in 2015.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: