Skip Headers

# Using Java

Previous

Next

To manually create a relationship query key, implement a descriptor amendment method (see "Configuring Amendment Methods") that uses one of the following `ClassDescriptor` methods to register the query keys:

- `addQueryKey`–specify a query key using an instance of `QueryKey` such as `DirectQueryKey`, `DirectCollectionQueryKey`, `ManyToManyQueryKey`, `OneToManyQueryKey`, or `OneToOneQueryKey`.

- `addDirectQueryKey`–add a query key that maps directly to the given database field.

- `addAbstractQueryKey`–add an abstract query key to an interface descriptor. Any implementors of that interface must define the query key defined by this abstract query key.

Examples (UNKNOWN STEP NUMBER) , (UNKNOWN STEP NUMBER) , and (UNKNOWN STEP NUMBER) illustrate how to define a query key in Java code.

Defining a Query Key

    // Add a query key for the foreign key field using the direct method
    descriptor.addDirectQueryKey("managerId", "MANAGER_ID");

    // The same query key can also be added through the addQueryKey method
    DirectQueryKey directQueryKey = new DirectQueryKey();
    directQueryKey.setName("managerId");
    directQueryKey.setFieldName("MANAGER_ID");
    descriptor.addQueryKey(directQueryKey);

    /* Add a one-to-one query key for the large project of which the employee is a leader (this assumes only one project) */
    OneToOneQueryKey projectQueryKey = new OneToOneQueryKey();
    projectQueryKey.setName("managedLargeProject");
    projectQueryKey.setReferenceClass(LargeProject.class);
    ExpressionBuilder builder = new ExpressionBuilder();
    projectQueryKey.setJoinCriteria(builder.getField(
        "PROJECT.LEADER_ID").equal(builder.getParameter("EMPLOYEE.EMP_ID")));
    descriptor.addQueryKey(projectQueryKey);

Defining a One-to-Many Query Key

    /* Add a one-to-many query key for the projects where the employee manages multiple projects */
    OneToManyQueryKey projectsQueryKey = new OneToManyQueryKey();
    projectsQueryKey.setName("managedProjects");
    projectsQueryKey.setReferenceClass(Project.class);
    ExpressionBuilder builder = new ExpressionBuilder();
    projectsQueryKey.setJoinCriteria(builder.getField(
        "PROJECT.LEADER_ID").equal(builder.getParameter("EMPLOYEE.EMP_ID")));
    descriptor.addQueryKey(projectsQueryKey);

Defining a Many-to-Many Query Key

    /* Add a many-to-many query key to an employee project that uses a join table*/
    ManyToManyQueryKey projectsQueryKey = new ManyToManyQueryKey();
    projectsQueryKey.setName("projects");
    projectsQueryKey.setReferenceClass(Project.class);
    ExpressionBuilder builder = new ExpressionBuilder();
    projectsQueryKey.setJoinCriteria(
        builder.getTable("EMP_PROJ").getField("EMP_ID").equal(
        builder.getParameter("EMPLOYEE.EMP_ID").and(
        builder.getTable("EMP_PROJ").getField("PROJ_ID").equal(
        builder.getField("PROJECT.PROJ_ID")));
    descriptor.addQueryKey(projectsQueryKey);

Example: Defining a One-to-One Query Key with an Amendment Method illustrates how to implement a `Descriptor` amendment method to define a one-to-one query key. In this example, the object model for the `Address` class does not include a reference to its owner, an `Employee` object. You can amend the `Address` class descriptor to add a query key named `owner` to make up for this deficiency. At run time, you can compose expressions that select `Address` objects based on this `owner` query key.

Defining a One-to-One Query Key with an Amendment Method

    /* Static amendment method in Address class, addresses do not know their owners in the object model, however you can still query on their owner if a user-defined query key is defined */
    public static void addToDescriptor(Descriptor descriptor) {
        OneToOneQueryKey ownerQueryKey = new OneToOneQueryKey();
        ownerQueryKey.setName("owner");
        ownerQueryKey.setReferenceClass(Employee.class);
        ExpressionBuilder builder = new ExpressionBuilder();
        ownerQueryKey.setJoinCriteria(
            builder.getField("EMPLOYEE.ADDRESS_ID").equal(
            builder.getParameter("ADDRESS.ADDRESS_ID")));
        descriptor.addQueryKey(ownerQueryKey);
    }