Retrieving a list of objects with their dependencies
In this recipe, we will add a DAO method to generate, from an SQL query joining several tables, a list of objects with their dependencies. We will retrieve a list of User objects along with their Post objects (blog posts written by these users).
Getting ready
You need to have model classes related to each other. In this example, a user has many posts:
public class User {
private Long id;
private String firstName;
private Integer age;
private LinkedList<Post> posts = new LinkedList<Post>();
public class Post {
private long id;
private String title;
private Date date;
private User user;You need to have corresponding database tables, for example:
CREATE TABLE `user` ( `id` int(11) AUTO_INCREMENT, `first_name` text, `age` int(11), PRIMARY KEY (`id`) ) CREATE TABLE `post` ( `id` int(11) AUTO_INCREMENT, `title` text, `date` datetime, `user_id` int(11), PRIMARY KEY (`id`), CONSTRAINT `user_id` FOREIGN...