In der Klasse wird die Verbindung zur Datenbank in der Methode doBeforeClass hergestellt. Die Methode test_joinTest1 testet die verschachtelten Beans, die über eine Implementierung generiert wurden, die eine SQL-Abfrage enthält. Die Methode test_joinTest2 testet die über eine Inline-SQL-Abfrage generierten verschachtelten Beans. Beide Methoden generieren Beans, die Daten aus der SQL-Beispielabfrage zurückgeben.
package mytest; import java.util.List; import java.sql.*; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.ibm.pdq.runtime.Data; import com.ibm.pdq.runtime.factory.DataFactory; /** * Basic tests nested beans with @JoinPoint and @JoinColumn annotations. */ public class NestedBeanJoinPointTest { private static Connection jdbcCon = null; @BeforeClass public static void doBeforeClass() throws SQLException, ClassNotFoundException { System.out.println("==start tests=="); System.out.println( com.ibm.pdq.tools.DataVersion.getVersion()); try { Class.forName("com.ibm.db2.jcc.DB2Driver"); jdbcCon = DriverManager.getConnection( "jdbc:db2://localhost:50000/GSDB:currentSchema=GOSALESHR;" + "retrieveMessagesFromServerOnGetMessage=true;", "testusr", "testpwd"); } catch (SQLException e) { System.out.println(e); } } @Test public void test_joinTest1() throws Exception { System.out.println("==Test1 nested beans with interface=="); SimpleQueryInterface data = null; data = DataFactory.getData(SimpleQueryInterface.class, jdbcCon); System.out.println("==Get Dept data =="); List<Dept1> depts = data.joinTest(); System.out.println("Employees by Dept:"); for (Dept1 d : depts) { System.out.println(d.getDeptCode() + " - " + d.getDeptNameEn()); List<Emp> employees = d.getDeptEmployees(); if (!employees.isEmpty()) { for (Emp e : employees) { System.out.println(" " + e.getLastName() + ", " + e.getFirstName() + ", " + e.toString()); } } else { System.out.println(" -- No Employees-- "); } } } @Test public void test_joinTest2() throws Exception { System.out.println("==Test2 nested bean with Inline SQL=="); Data data = null; data = DataFactory.getData(jdbcCon); List<Dept2> depts = data queryList( "SELECT E.EMPLOYEE_CODE, E.FIRST_NAME, E.LAST_NAME, E.DATE_HIRED, " + " ORG.ORGANIZATION_CODE, ORG.ORGANIZATION_NAME_EN" + " FROM EMPLOYEE AS E, EMPLOYEE_HISTORY AS EH, ORGANIZATION AS ORG" + " WHERE EH.EMPLOYEE_CODE = E.EMPLOYEE_CODE " + " AND EH.ORGANIZATION_CODE = ORG.ORGANIZATION_CODE" + " AND EH.RECORD_END_DATE IS NULL " + " ORDER BY EH.ORGANIZATION_CODE, E.LAST_NAME ", Dept2.class); System.out.println("Employees by Dept:"); for (Dept2 d : depts) { System.out.println(d.deptCode + " - " + d.deptNameEn); List<Emp> employees = d.deptEmployees; if (!employees.isEmpty()) { for (Emp e : employees) { System.out.println(" " + e.getLastName() + ", " + e.getFirstName() + ", " + e.toString()); } } else { System.out.println(" -- No Employees-- "); } } } }