Search code examples
jpa-2.0eclipselinkcriteria-api

Criteria API: can I access a given alias by its name


Is it possible with Criteria API to access a given alias via the Root object?

I have defined a Join and gave it the alias "lead":

final Root<Project> project = cq.from(Project.class);
project.join(Project_.lead).alias("lead");

How can i gain access to that Join given just the Root and the alias (because Root is the only query handle, that I pass around)?

I've already tried to rebuild the Join by navigating again, but that seems to break the SQL statement:

    project.join(Project_.lead) // cannot do this

Thanks


Solution

  • From does have method getJoins and TupleElement method getAlias. Root implements them, so following should work:

    //TODO: apply generics; argument do not have to be root, but
    //      something that implements From and TupleElement
    private Join findJoin(Root root, String alias) {
        Set<Join> joins= root.getJoins();
        for (Join join: joins) {
            if (alias.equals(join.getAlias())) {
                return join;
            }
        }
        throw new IllegalArgumentException("No join for alias:" + alias);
    }