javajpacriteria-apijpa-criteria

Explain the meaning of JPA Criteria API From class and its type parameters


What is the purpose of class javax.persistence.criteria.From and what do its type parameters Z and X stand for ?

The documentation is not clear at all.

I became even more confused after I saw that the type javax.persistence.criteria.Root has the following definition:

public interface Root<X> extends From<X,X>


Solution

  • It is a common interface for: javax.persistence.criteria.Join and javax.persistence.criteria.Root to allow consistency when you call one of the methods:

    Let's try explain using javax.persistence.criteria.Join#getParent When You call javax.persistence.criteria.Join#getParent result can be another Join or Root and a common interface for both is javax.persistence.criteria.From

    See example:

    Root<User> user = query.from(User.class);
    Join<User, Account> account = user.join(User_.account);
    Join<Account, AccountRole> accountRole = account.join(Account_.accountRoles);
    

    then

    From<?, User> root = account.getParent();// here we have Root<User> extends From<User, User>
    From<?, Account> join = accountRole.getParent();// here we have Join<User, Account> extends From<User, Account>
    

    And as You can see in the above example Z and X stand for a type of left and right side of the join.