August 12 update: it seems that some people link directly to this post and do not see in later posts that this project has been published on Google Code: http://code.google.com/p/activejdbc/
Original post text follows:
This blog is not really a tutorial, but rather a high level overview of some important features this framework has. As I stated in a previous post, I really bent backwards when implementing it, only to make it easier for developers to access persistent data.
I will present various use cases in a list format:
How to run a simple query
//find by id: Person p = Person.findById(0); //find first: Person p = Person.first("name = ?", "John"); //simple select of multiples: List<Person> people = Person.where("department = ? and hire_date > ? ", "IT", hireDate); //...iterate
How to build pageable resultsets
List<Employee> people = Employee.where("department = ? and hire_date > ? ", "IT", hireDate) .offset(21) .limit(10) .orderBy("hire_date asc"); ...iterate
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.
I tried these queries on tables with millions of records on Oracle and performance is flat.
In fact, you can learn how to create queries like this if ActiveJDBC logging is enabled.
How to create new records
Person p = new Person(); p.set("name", "Marilyn"); p.set("last_name", "Monroe"); p.set("dob", "1935-12-06"); p.saveIt();
This code should be self explanatory. As you can see, ActiveJDBC does not require to have getters and setters. You can write them, if you like, but IMHO, they are nothing but code pollution.
The
set(name, value)
method returns reference to the same model object, which makes it possible to string method calls like this:Person p = new Person(); p.set("name", "Marilyn").set("last_name", "Monroe").set("dob", "1935-12-06").saveIt();
There is even a third way to set values into a model:
String[] names = {"first_name", "last_name", "dob"}; Object[] values = {"John", "Doe", new Date(johnsDobTime)} Person john = new Person(); john.set(names, values).saveIt();...and yet another way to set values into a model is with a use of a map:
Map values = ... initialize map Person p = new Person(); p.fromMap(values); p.saveIt();
I hope this was entertaining. I will write more about features of ActiveJDBC in future posts. Specifically how it handles relationships.
Constructive feedback is much appreciated!
Have fun :)