Over a million developers have joined DZone.

ActiveJDBC: new Java ORM and an ActiveRecord implementation

DZone's Guide to

ActiveJDBC: new Java ORM and an ActiveRecord implementation

Free Resource
When I learned Ruby on Rails, I was first and foremost impressed with ActiveRecord. For those who do not know ActiveRecord, it is a Ruby on Rails' ORM layer, which is an equivalent to Hibernate in Java. However, ActiveRecord is easier to work with because it is based on conventions rather than configuration, and also it configures itselt at start time based on structure of data in the database. I was so impressed with ActiveRecord, that I was sure someone would implement it in Java, but after waiting a coupls of years, I put a stop to wait and implemented it myself (with help of a few friends).

ActiveJDBC is a fast and lightweiht library for accessing relational databases. Its syntax is concise and easy to understand. The library is based on following principals:

  • No configuration, just conventions
  • No need to learn another QL - SQL is sufficient
  • Code must be lightweight and intuitive, should read like English
  • No sessions, no "attaching, re-attaching"
  • No persistence managers.
  • No proxying. What you write is what you get (WYWIWYG :))
ActiveJDBC has no configuration; it maps models to tables automatically. Here is an example:
public class Person extends Model{}
This is a fully functioning class for accessing a table PEOPLE. The following code works right out of the box:
List retirees = Person.where("age > ?", age);
Since ActiveJDBC does not introduce a new query language, the snippet "age > ?" is a true SQL snippet which usually follows the "WHERE" clause. ActiveJDBC does not require setter/getter methods for mapping to columns. Instead, it provides a single get and a single set method:
String firstName = person.get("first_name");
person.set("last_name", lastName);
Writing setters and getters is possible for convenience:
public Sting getFirstName(){
return get("first_name");
ActiveJDBC supports three types of relationships: one to many, many to many and polymorphic. In a one to many relationship, many records from a "child" table relate to one record in the parent table. Exampes are: customer and addresses, order and line items, etc. When ActibeJDBC starts up, it undergoes a disovery phase, when it examines database metadata (tables, columns, relationships) and maps model classes to tables. For example, if there is a table CUSTOMERS and a table ADDRESSES, and a table ADDRESSES has a column "CUSTOMER_ID", then you can write code like this without any additional configuration:
Customer c = Customer.findById(123);
addresses = c.getAll(Address.class);

Many to many and polymorphic associations are supported similarly. ActiveJDBC is full featured and has been around since 2009. If you are interested to learn more, follow the link to the project site:http://code.google.com/p/activejdbc/




Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}