ActiveJDBC
Fast ORM for agile development
Documentation
Follow to: ActiveJDBC documentation
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,
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:
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)
.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("first_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:
and yet shorter one :
Updating a record
Employee e = Employee.findFirst("first_name = ?", "John");
e.set("last_name", "Steinbeck").saveIt();
Deleting a record
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:
- See 5 minute introduction
- Familiarize yourself with Instrumentation
Other working examples:
- Maven example
- Ant example
- Standalone example - neither Maven nor Ant
Here is the JavaDoc
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 2019
- MariaDB (10.5.8)
- Oracle (11.2.0.2.0)
- PostgreSQL (13.1)
- H2
- SQLite3
- DB2 (11.5.4.0)
The versions listed above are what we use for testing. There is no reason to think that ActiveJDBC does not support a different version, after all it is a pass-through framework.
Adding a new dialect is relatively easy. Just look at commits on this branch: h2integration
Releases
Please, refer to the Releases page.
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>snapshots1</id>
<name>Sonatype Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sonatype_plugin_snapshots</id>
<name>Sonatype Plugin Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
If you are not using Maven, you can pull down the latest snapshots from here: Sonatype Snapshots
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