TopLink supports query keys for relationship mappings and automatically defines them for all relationship mappings. You can use these keys to join across a relationship. One-to-one query keys define a joining relationship and are accessed through the get()
method in expressions.
One-to-many and many-to-many query keys define a distinct join across a collection relationship and are accessed through the anyOf()
method in expressions. You can also define relationship query keys manually if mapping
does not exist for the relationship. The relationship defined by the query key is data-level expressions.
Example 4-4 One-to-one Query Key
The following code example illustrates using a one-to-one query key within the TopLink expression framework
ExpressionBuilder employee = new ExpressionBuilder();
Vector employees = session.readAllObjects(Employee.class, employee.get("address").get("city").equal("Ottawa"));
Relationship query keys are not supported directly in the TopLink Mapping editor. To define a relationship query key, you must specify and write an amendment method. Register query keys by sending the addQueryKey()
message.
Example 4-5 Defining One-to-one Query Key Example
The following code example illustrates how to define a one-to-one query key.
// 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);
}
Copyright © 1997, 2004, Oracle. All rights reserved.