ActiveJDBC: an implementation of the Active Record pattern for Java

Igor Polevoy, who I’ve had the pleasure of working with on a couple projects, has just posted his ActiveJDBC project to Google code.  If you work on Java applications that access relational databases, you owe it to yourself to check it out.

ActiveJDBC implements the Active Record pattern, initially documented by Martin Fowler in his book Patterns of Enterprise Application Architecture and popularized by Ruby on Rails.

What’s interesting about ActiveJDBC is how little baggage it brings with it, and how quickly one can get started using it.  It’s pure Java, requires few other libraries (no Hibernate, JPA, etc), and requires no configuration (no mapping, etc).  All metadata is inferred from the database schema.

Some examples of ActiveJDBC in action:

Persistent Book class definition:

import activejdbc.Model;

public class Book extends Model {}

Adding a new Book:

Book b = new Book();
b.set("isbn", "0321127420")
  .set("title", "Patterns of Enterprise Application Architecture")
  .set("author", "Martin Fowler")
  .set("publisher", "Addison-Wesley Professional");
b.saveIt();

Paging through Addison-Wesley books in sorted order

List<Book> books = Book.where("publisher = ?", ""Addison-Wesley Professional");
  .offset(11)
  .limit(10)
  .orderBy("author asc");

It looks like this framework strikes a good balance between a simple framework that works as you expect, and providing an elegant DSL for database access.  This is particularly impressive given Java as a language doesn’t lend itself to implementing DSLs.

One thing I was curious about but didn’t see an example of, was working with associations, which is listed on the features page.

For more info, see the Getting Started Guide on Google Code, Igor’s blog, and the Productive Edge Java blog.

This entry was posted in Java. Bookmark the permalink.

1 Response to ActiveJDBC: an implementation of the Active Record pattern for Java

  1. Igor Polevoy says:

    Ron, you hit the right chord when you said: “this framework strikes a good balance between a simple framework that works as you expect, and providing an elegant DSL for database access.”

    This is exactly what I was targeting. As far as documentation, I will be publishing it in the next few days. The article on how one to many associations work has already been done, please see here: http://code.google.com/p/activejdbc/wiki/OneToManyAssociation

    I hope this too “strikes a good balance” 🙂

    Cheers,
    Igor

Leave a Reply

Your email address will not be published. Required fields are marked *