# Using View Links in Multi-Table Relationships in Code

The following code example assumes that a one-many-many relationship was defined at design time where the Application Module links views of customers to orders and orders to items. Thus, the Application Module uses the `Orders` entity twice: once as the destination end of the customer-orders link, and once as the source end of the order-items link. The code loads links from the Application Module, creates views based on those links, then iterates over the result sets to display sales histories for selected customers. Iterating through the customer view refreshes the orders view, which in turn refreshes the items view, all automatically.

    package omm;
    import oracle.jbo.*;

    public class MultiLinkDemo {
      public static void main(String[] args) {
        String platform = JboContext.PLATFORM_LOCAL;

        // appModName identifies an Application Module defined at design time.
        // Format: <package>.<name>
        String appModName = "omm.ommModule";

       // Helper routine connects to the generic Application Module.
        ApplicationModule appMod =
           QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);

        // Get View Links defined at design time.
        // CustToOrd links CustomerView to SalesOrderView by CustomerId.
        ViewLink custOrdLink = appMod.findViewLink("CustToOrd");

        // OrdToItem links SalesOrderView to ItemView by OrderId.
        ViewLink ordItemLink = appMod.findViewLink("OrdToItem");

        // Get View Objects defined in the links.
        ViewObject voCust = custOrdLink.getSource();
        ViewObject voOrd  = custOrdLink.getDestination();
        ViewObject voItem = ordItemLink.getDestination();

        // There are a lot of customers. We'll just get the first three.
        voCust.setRangeSize(3);
        Row[] custRows = voCust.getAllRowsInRange();

        // Move through the rows.
        String invoice = "";

        for (int i = 0; i < custRows.length; i++) {
          invoice = "\n----- SALES HISTORY ------\n";
          invoice += getRowData(voCust.next());
          while (voOrd.hasNext()) {
            invoice += "\t" + getRowData(voOrd.next());
            while (voItem.hasNext()) {
              invoice += "\t\t" + getRowData(voItem.next());
            }
          }

        System.out.println(invoice);
        }

        System.out.println("\nPress Enter to close this window.");
        try {System.in.read();} catch (Exception e) {}

      }

      // This is a helper method that prints data to the screen.
      public static String getRowData(Row row) {

          String rowAttrs = "";
          for (int i = 0; i < row.getAttributeCount() - 1; i++) {
            rowAttrs += (row.getAttribute(i) + "\t");
          }
          return rowAttrs + "\n";
      }

    }