"? I saw a question about Builder criteria and came up with this question ... What does it mean from.

What does this mean: "from. <Integer>"?

I saw a question about Builder criteria and came up with this question ...

What does it mean from.<Integer> get("...?

I have never seen a point of expression <Integer>.

Can someone show me an example?

Link: Compile error when using CriteriaBuilder

Code:

public List<BankAccount> findWithBalance(int amount) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<BankAccount> cq = cb.createQuery(BankAccount.class);
    Root<BankAccount> from = cq.from(BankAccount.class);

    ParameterExpression<Integer> balance = cb.parameter(Integer.class);
    cq.select(from);

    //Here is the trick!
    Predicate predicate = cb.gt(from.<Integer> get("balance"), balance);

    cq.where(predicate);
    cq.orderBy(cb.asc(from.get("ownerName")));

    TypedQuery<BankAccount> query = em.createQuery(cq);

    query.setParameter(balance, amount);

    return query.getResultList();
}

Thanks!

+4
source share
3 answers

Java-. , . . . , :

void processStringList(List<String> stringList) {
    // process stringList
}

processStringList(Collections.emptyList()); // Compile error!
processStringList(Collections.<String>emptyList());  // Ok!

-, Java 8 , .

+2

Java Generics and Collections,

List<Integer> ints = Lists.<Integer>toList(); // first example
List<Object> objs = Lists.<Object>toList(1, "two"); // second example
  • In the first example, , Sun . toList , , , . ( Eclipse .)
  • In the second example, . , Object , , Serializable Comparable. , .

:

, , , , ; , .

, , , .

Java , , . toList , , :

List<Integer> ints = <Integer>toList(); // compile-time error

, .

0

Source: https://habr.com/ru/post/1523414/


All Articles