This is a guest post by Igor Polevoy (blog, @ipolevoy). Igor is a Chief Architect at Productive Edge, LLC.
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 couple of years, I put a stop to wait and implemented it myself (with help of a few friends).
ActiveJDBC is a fast and lightweight 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 the PEOPLE table. 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/
After years of “complicated” enterprise Java, things are starting to improve, one of the main directions in enterprise Java today is to make things simple. I think persistence is still more complicated than it should be. ORM frameworks such as Hibernate or JPA, although very powerful (and appropriate in some projects), are also complicated to understand and learn and don’t always “make your development simpler” (JPA has almost 500 page specification document). ActiveJDBS is very simple and elegant persistence framework. My guess is that more and more projects will consider using simple and elegant persistence solutions such as ActiveJDBS.
Leave a Reply