SQL Zone is brought to you in partnership with:

Igor has posted 1 posts at DZone. View Full User Profile

ActiveJDBC: new Java ORM and an ActiveRecord implementation

07.11.2011
| 3127 views |
  • submit to reddit
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);
List 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/

enjoy,

Igor

0
Average: 4 (1 vote)
Published at DZone with permission of its author, Igor Polevoy.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Igor Polevoy replied on Wed, 2011/07/13 - 4:45pm

folks, the formating of the article does not work for some reason, please click
Printer-friendly version

for ease of reading

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.