Ryan
Ryan

Reputation: 1360

BETWEEN query with JPA and Metamodel

I am trying to write a between query with JPA 2.

Integer zipCode = 50000;
CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Territory> query = builder.createQuery(Territory.class);
Metamodel m = getEntityManager().getMetamodel();

Root<Territory> root = query.from(Territory.class);

// zipCode between startZipCodeRange and endZipCodeRange
Predicate condition = builder.between(zipCode , root.get(Territory_.startZipCodeRange), root.get(Territory_.endZipCodeRange));

The final line does not compile because zipCode is not of type Expression. Is it possible to convert zipCode to an Expression? If so, how would I do that.

Upvotes: 1

Views: 1486

Answers (1)

Raman
Raman

Reputation: 1527

You can use root.get(Territory_.zipcode) as 1st parameter of between function if, Territory does have zipcode attribute.

See the comments on this answer for more details.

Upvotes: 1

Related Questions