Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Querying/Query Keys

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


Query Keys in Queries

As of EclipseLink 2.1, it is possible to reference manually-defined query keys within JPA queries. Prior to this release, such query keys could only be referenced in native queries using expressions.

By default, EclipseLink creates query keys for all mapped attributes, but in some scenarios you may find it beneficial to add your own.

The following example shows...

public class EmployeeCustomizer implements DescriptorCustomizer {
 
    @Override
    public void customize(ClassDescriptor descriptor) throws Exception {
        // Direct (basic) query for the Oracle DB's ROWID value
        descriptor.addDirectQueryKey("rowId", "ROWID");
 
        // 1:M query accessing all projects which have a M:1 teamLeader reference to this employee
        // this can also be used to allow querying of large collections not wanted mapped
        OneToManyQueryKey projectsQueryKey = new OneToManyQueryKey();
        projectsQueryKey.setName("leadProjects");
        projectsQueryKey.setReferenceClass(Project.class);
        ExpressionBuilder builder = new ExpressionBuilder();
        projectsQueryKey.setJoinCriteria(builder.getField("PROJECT.LEADER_ID").equal(builder.getParameter("EMPLOYEE.EMP_ID")));
        descriptor.addQueryKey(projectsQueryKey);
    }
 
}

Then the query key can be references in the query in the same way any mapped attribute is.

TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE e.leadProjects IS NOT EMPTY", Employee.class);
return query.getResultList();


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top