简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM