Relations with JDBC 4.0
The best way to see what JDBC 4.0 is going to offer is to look at DataSet<T>'s documentation. There are some examples there that really make you want to try it out, even if you don't work with direct database access at the moment.
For me - I can't, and this relates to my on going rant about the fact that the beta of the new Java platforms is never released for Macs. I guess I should just install a Linux machine and get on with it. Until then, I can only theoritically talk about the coming Mustang.
Now, about JDBC 4.0 - It offers great simplicity for data traversal, encapsulating the data retrieved from the database for you inside struct-like objects. However, at this point there is no option to automagically define a relation between two of these classes and have JDBC do the plumbing for you.
Since someone asked me, I replied with a pattern I think should work, and I will try it out as soon as I actually install a Linux machine. This isn't magic, it's just using what JDBC 4.0 is giving. But it does give a nice make-over to code to look a bit more self-contained.
For example, suppose you have a
Classroom class which contains a lot of Student classes. To get all the students via the Classroom class:
class Student {
int id;
String name;
}
class Classroom {
int number;
String name;
DataSet<Student> getStudents(SchoolDAL dal) {
return dal.getStudentsForClassroom(number);
}
boolean addStudent(SchoolDAL dal, Student s) {
return dal.insertStudent(s.id, s.name, number) > 0;
}
}
interface SchoolDAL extends QueryBase {
@Select("select number, name from classrooms")
DataSet<Classroom> getAllClassrooms();
@Select("select id, name from students where classroom_id = ?1")
DataSet<Classroom> getStudentsForClassroom(int classroom);
@Update("insert into students (id, name, classroom_id) values (?1, ?2, ?3)")
int insertStudent(int id, String name, int classroom);
}
This can make the Classroom instances be more self-contained, so that traversal over the instances and their sub-instances would not require calling external classes' methods. If there was only a way to get the QueryBase-implemented interface into the iterated instances, it would also eliminate the dependency this sort of implementation has on it (by passing SchoolDAL to the getStudents and addStudent methods).