ActiveJDBC

You often write small tools for processing data from databases? You like the way GORM provides access to database in Grails? You don’t want to use these heavyweight Hibernate/TopLink/iBatis? JPA is too enterprisy for you? Then you definetely need to have a look at ActiveJDBC.

To integrate it into your Maven build just add the following dependencies in dependencies section:

<dependency>
  <groupId>activejdbc</groupId>
  <artifactId>activejdbc</artifactId>
  <version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>8.4-702.jdbc4</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.5.10</version>
</dependency>

I’ve added PostgreSQL JDBC driver in order test ActiveJDBC against the real database.
You’ll also need the following dependency in plugins section:

<plugin>
  <groupId>activejdbc</groupId>
  <artifactId>activejdbc-instrumentation</artifactId>
  <version>1.1-SNAPSHOT</version>
  <executions>
  <execution>
    <id>execution</id>
    <phase>process-classes</phase>
    <goals>
    <goal>instrument</goal>
    </goals>
  </execution>
  </executions>
</plugin>

ActiveJDBC operates with the so called models. To make your class persistent just inherit it from activejdbc.Model.

import activejdbc.Model;

public class TestModel extends Model {
}

Next thing you need to do – is to create table in the database.

create table test_models(id serial primary key, test varchar(255));

We’re ready to start playing with ActiveJDBC. Lets connect database:

Base.open("org.postgresql.Driver", "jdbc:postgresql:test", "testUsr", "testPwd");

Now we can open transaction and commit/rollback it after doing some job:

Base.openTransaction();
... some job ...
Base.commitTransaction();

Lets create a new object:

TestModel.createIt(new Object[]{"test", "qbe"})

After this – a new object is stored in the database. I haven’t seen anything simpler to work with the database, have you?
Lets get the count of the items:

TestModel.count();

We can also find item by id:

TestModel.findById(Integer.valueOf(4));

Why not try to do search all items against the database:

List data = TestModel.find("test like ?", new Object[] {"%7%"});

or just the first one:

TestModel model = (TestModel) TestModel.findFirst("test like ?", new Object[] {"%7%"});

ActiveJDBC uses maps underneath and approach here is very similar to GORM. The basic CRUD operations look like:

TestModel model = new TestModel();
model.setString("test", "hello!");
model.saveIt();
model.delete();

saveIt method creates a new row in database or updates an existing one if value id attribute is specified. In case of using saveIt method – an exception will be thrown in case of problems. In case of using save method – no exception is thrown, instead a field errors is populated.

You can add your custom validator for the model objects. You can easily serialize object into JSON or XML:

model.toJson(true, new String[]{"test"});
model.toXml(1, true, new String[]{"test"});

It’s a great, really great library. May be it’s not well suited for big projects but it’s really perfect for small everyday tasks. Looks like ActiveJDBC is not mature enough and has much place to grow but idea of having such a thing in plain Java is really increadible.