3

How are the two following CriteriaBuilder methods used? Have you ever used them or seen them used in a real world application? I can't find an example on the web. Reading the code comments: values and keys creates an collection expression that can be passed to size(), isMember(), etc.

//get the values and keys collections of the Map, which may then
//be passed to size(), isMember(), isEmpty(), etc

/**
 * Create an expression that returns the values of a map.
 *
 * @param map map
 *
 * @return collection expression
 */
<V, M extends Map<?, V>> Expression<Collection<V>> values(M map);

/**
 * Create an expression that returns the keys of a map.
 *
 * @param map map
 *
 * @return set expression
 */
<K, M extends Map<K, ?>> Expression<Set<K>> keys(M map);

But why would JPA be needed to determine the size of a collection, or to test whether an element is a member of a collection? It seems that this can be done in Java, no need for JPA.

Peter Šály
  • 2,848
  • 2
  • 12
  • 26
Patrick Garner
  • 3,201
  • 6
  • 39
  • 58

1 Answers1

0

I will just answer 2nd question.

Why would JPA be needed to determine the size of a collection, or to test whether an element is a member of a collection? It seems that this can be done in Java, no need for JPA.

These JPA methods are used for building queries. Creating condition before retrieving data from DB is more efficient as to load all data and then filter it in Java.

For example user has more roles. We want to load users with specific role. For this member of JPQL reserved word is used.

select u from User u where :specificRole member of u.roles

Same way size(), is empty can be used. https://www.objectdb.com/java/jpa/query/jpql/collection

Peter Šály
  • 2,848
  • 2
  • 12
  • 26