ActiveJDBC - Fast Java ORM

Design principles

Getting started

If you want to get started, follow these links:

Please, see Getting Started for a working example.

Look through these:

Other working examples:

Here is JavaDoc for ActiveJDBC

Documentation

Collapse table of contents

5 minute introduction

For a simple example we will use a table called PEOPLE created with this MySQL DDL:

CREATE TABLE people (
  id  int(11) NOT NULL auto_increment PRIMARY KEY,
  name VARCHAR(56) NOT NULL,
  last_name VARCHAR(56),
  dob DATE,
  graduation_date DATE,
  created_at DATETIME,
  updated_at DATETIME
  );

ActiveJDBC infers DB schema parameters from a database. This means you do not have to provide it in code. The simplest model looks like this:

public class Person extends Model {}

Despite the fact that there is no code in the body of the class, it is fully functional and will map to a table called PEOPLE automatically.

How to query

Simplest query example:

List<Person> people = Person.where("name = 'John'");
Person aJohn = people.get(0);
String johnsLastName = aJohn.get("last_name");

The where() method takes a snippet of real SQL. The first line above will generate this statement: SELECT * FROM people WHERE name = 'John'.

Finder methods can also be parametrized:

List<Person> people = Person.where("name = ?", "John");
Person aJohn = people.get(0);
String johnsLastName = aJohn.get("last_name");

The where() method takes a vararg, allowing unlimited any number of parameters to be passed into a statement.

Paging through data

List<Employee> people = Employee.where("department = ? and hire_date > ? ", "IT", hireDate)
    .offset(21)
    .limit(10)
    .orderBy("hire_date asc");

This query will ensure that the returned result set will start at the 21st record and will return only 10 records, according to the orderBy. The ActiveJDBC has a built in facility for various database flavors and it will generate appropriate SQL statement that is specific for a DB (Oracle, MySQL, etc) and is efficient. It will not fetch all records, starting with 1.

Creating new records

There are several ways to do this, and the simplest is:

Person p = new Person();
p.set("name", "Marilyn");
p.set("last_name", "Monroe");
p.set("dob", "1935-12-06");
p.saveIt();

There is also a shorthand version of doing the same:

new Person().set("name", "Marilyn").set("last_name", "Monroe").set("dob", "1935-12-06").saveIt();

and yet shorter one :

Person.createIt("name", "Marilyn", "last_name", "Monroe", "dob", "1935-12-06");

Updating a record

Employee e = Employee.findFirst("first_name = ?", "John");
e.set("last_name", "Steinbeck").saveIt();

Deleting a record

Employee e = Employee.findFirst("first_name = ?", "John");
e.delete();

There are more ways to delete, follow Delete cascade to learn more.

Getting the latest version

For the latest version refer to ActiveJDBC on Maven Central. If you use Maven add this to your pom:

<dependency>
    <groupId>org.javalite</groupId>
    <artifactId>activejdbc</artifactId>
    <version>LATEST_VERSION</version>
</dependency>

Do not forget to replace LATEST_VERSION with the latest version deployed to Maven Central (see above)

Additionally, configure Instrumentation plugin:

<plugin>
    <groupId>org.javalite</groupId>
    <artifactId>activejdbc-instrumentation</artifactId>
    <version>LATEST_VERSION</version>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>instrument</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Or download from: ActiveJDBC Instrumentation plugin on Maven Central

Getting latest snapshot versions

If you are using Maven, add these repositories to your pom:

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Plugin Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>sonatype-nexus-plugin-snapshots</id>
        <name>Sonatype Nexus Plugin Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

If you are not using Maven, you can pull down the latest snapshots from here: Sonatype Snapshots