Follow to: ActiveJDBC documentation

5 minute introduction

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

  id  int(11) NOT NULL auto_increment PRIMARY KEY,
  first_name VARCHAR(56) NOT NULL,
  last_name VARCHAR(56),
  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 corresponding model looks like this:

public class Person extends Model {}

There is no code in the body of the class, and yet it is fully functional and will map to a table called PEOPLE automatically. Read more on English Inflections.

How to query

Simplest query example:

List<Person> people = Person.where("first_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 first_name = 'John'.

Finder methods can also be parametrized:

List<Person> people = Person.where("first_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)
    .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("first_name", "Marilyn");
p.set("last_name", "Monroe");
p.set("dob", "1935-12-06");

There is also a shorthand version of doing the same:

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

and yet shorter one :

Person.createIt("first_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");

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

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 the JavaDoc for ActiveJDBC

Design principles

  • Infers metadata from DB
  • Convention-based configuration.
  • No need to learn another QL. SQL is sufficient
  • Code often reads like English
  • No sessions, no "attaching, re-attaching"
  • No persistence managers.
  • Models are lightweight, simple POJOs
  • No proxy-ing. What you write is what you get (WYWIWYG :))
  • No getters and setters. You can still write them if you like.
  • No DAOs and DTOs
  • No Anemic Domain Model

Supported databases

Currently the following databases are supported:

  • SQLServer
  • MySQL
  • Oracle
  • PostgreSQL
  • H2
  • SQLite3

Adding a new dialect is relatively easy. Just look at commits on this branch: h2integration


Please, refer to ActiveJDBC Releases.

Getting the latest version

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


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

Current latest version: 1.4.12.

Additionally, configure Instrumentation 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:

        <name>JavaLite Snapshots</name>
        <name>JavaLite Plugin Snapshots</name>

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

How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page