Design principles


Collapse table of contents

Programming model

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 example model looks like this:
public class Person extends Model {}
Despite the fact that there is no code in it, it is fully functional and will map to a table called PEOPLE automatically. Here is how to use it:

How to query

Querying is easy:
List people = Person.where("name = 'John'");
Person aJohn = people.get(0);
String johnsLastName = aJohn.get("last_name");
As you can see, the amount of code is reduced to a level when it is actually readable. Finder methods can also be parametrized like this:
List people = Person.where("name = ?", "John");
    Person aJohn = people.get(0);
    String johnsLastName = aJohn.get("last_name");
    Paging through data
    List 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("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("name", "Marilyn").set("last_name", "Monroe").set("dob", "1935-12-06").saveIt();
and yet another 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");

Getting started

Here are some simple example applications to help you get started:

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)

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:
            Sonatype Nexus Plugin Snapshots

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