Returning more than one bean when using annotated methods

If a SELECT statement queries more than one database object and you are using annotated methods, you can return the data from each object in a separate bean.

Procedure

To return more than one bean for a single SELECT statement that queries more than one database object:

  1. Declare a generic class that extends either LinkedList or ArrayList. The number of objects that this parameterized collection contains defines the number of beans that are returned from your query.
  2. In the signature of your method, specify the return type as either an Iterator or List object. Specify the parameterized collection as the type for that object.

Example

You want to run this statement, returning the result from the first table in a Contact bean and the result from the second table into a Person bean.

select CONTACTTABLE.IDNUMBER, PERSONTABLE.SSN from CONTACTTABLE, PERSONTABLE

You can define the generic class ContactAndPerson like this:

public class ContactAndPerson<C, P> extends LinkedList{}

You want to put all pairs of Contact and Person beans into an Iterator object. So, you define the return type like this:

@Select(sql = "select CONTACTTABLE.IDNUMBER, PERSONTABLE.SSN from CONTACTTABLE, PERSONTABLE")  
Iterator<ContactAndPerson<Contact, Person>> selectFromContactPerson ();

Feedback