Question

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

Was it helpful?

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top