Say my DB looks like this, presenting using POJO:
class A {
long id; // auto-increment primary key
String aAttribute;
}
class B {
long id; // auto-increment primary key
long aId; // foreign key of class A
String bAttribute;
}
How could I naturally map the DB records to class B using JDBI so the class B could contain the actual object of A instead of a foreign key to A:
class B {
long id; // auto-increment primary key
A a; // actual object of class A
String bAttribute;
}
One approach (there are others, also) is to use the JDBI
@Nestedannotation with a bean mapper. The annotation:Place the annotation on the relevant setter (or getter). So, in your case that would be like this:
I have also added
@ColumnNameannotations to disambiguate the otherwise identical column names between the two objects (and, presumably, the tables).Here is class A:
A query therefore needs to use column aliases to match these annotations:
Each instance of class B in the resulting list will contain an instance of A (assuming the data exists in the database).