Skip Headers

# Using Java

Previous

Next

You can use `ObjectLevelReadQuery` API to add joined attributes for one-to-one and one-to-many relationships.

Use the `ObjectLevelReadQuery` method `addJoinedAttribute(Expression attributeExpression)` to add join expressions to the query. Using this method, you can add multiple joined attributes for one-to-one and one-to-many relationships, including nested joins. The source and target can be the same class type. You cannot use the `ObjectLevelReadQuery` method `addJoinedAttribute` with a join expression on a many-to-many mapped attribute.

- Use the `ObjectLevelReadQuery` method `addJoinedAttribute` with a join expression on a one-to-one mapped attribute to get the class of the `ObjectLevelReadQuery` and the target of the one-to-one mapped attribute of that class with a single database hit.

- Use the `ObjectLevelReadQuery` method `addJoinedAttribute` with a join expression on a one-to-many mapped attribute to get the class of the `ObjectLevelReadQuery` and the target collection of the one-to-many mapped attribute of that class with a single database hit.

Example: Join Reading Multiple Attributes is based on the TopLink `ThreeTierEmployee` example project. It shows a `ReadAllQuery` configured to join-read multiple attributes. This query produces the SQL that Example: SQL for Multiple Attribute Join Reading shows.

Join Reading Multiple Attributes

    ReadAllQuery query = new ReadAllQuery(Employee.class);

    Expression managedEmployees = query.getExpressionBuilder().anyOfAllowingNone(
        "managedEmployees"
    );
    query.addJoinedAttribute(managedEmployees);
    query.addJoinedAttribute(managedEmployees.get("address"));
    query.addJoinedAttribute(managedEmployees.anyOf("phoneNumbers"));

    Vector employees = (Vector)getSession().executeQuery(query);

SQL for Multiple Attribute Join Reading

    SELECT DISTINCT
        t2.VERSION, t3.EMP_ID, t2.GENDER, t3.SALARY, t2.EMP_ID, t2.F_NAME, t2.L_NAME,
        t2.MANAGER_ID, t2.ADDR_ID, t2.END_DATE, t2.START_DATE, t2.END_TIME,
        t2.START_TIME, t0.VERSION, t1.EMP_ID, t0.GENDER, t1.SALARY, t0.EMP_ID,
        t0.F_NAME, t0.L_NAME, t0.MANAGER_ID, t0.ADDR_ID, t0.END_DATE, t0.START_DATE,
        t0.END_TIME, t0.START_TIME
    FROM
        SALARY t3, EMPLOYEE t2, SALARY t1, EMPLOYEE t0
    WHERE
        ((t3.EMP_ID = t2.EMP_ID) AND
         ((t0.MANAGER_ID (+) = t2.EMP_ID) AND
         (t1.EMP_ID (+) = t0.EMP_ID)))

Use the `ObjectLevelReadQuery` method `addJoinedAttribute(java.lang.String attributeName)` to configure the query to join-read a single attribute, as Example: Join Reading a Single Attribute shows.

Join Reading a Single Attribute

    ReadAllQuery query = new ReadAllQuery(Employee.class);
    query.addJoinedAttribute("address");
    Vector employees = (Vector)getSession().executeQuery(query);