I'm trying to check if a column in the db is not an empty string or not null but I can't figure out how to do it with criteria builder queries in order to get actual objects back. This sql works:
sampleName is not null and sampleName != ''
But when I try to do it with criteria builder like this:
// this is in a private method filterBySampleNotEmpty
cb.notEqual(root.get("sampleName"), "");
called by this in another method
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Sample> query = criteriaBuilder.createQuery(Sample.class);
Root model = query.from(Sample.class);
List<Predicate> predicates = new ArrayList<>();
predicates.add(filterBySampleNotEmpty(model, criteriaBuilder));
query.select(model).where(predicates.toArray(new Predicate[]{}));
It returns the whole list still.
Try to use this code:
criteriaBuilder.isNotNull(model.get(entity.name))
Please make a try with the following
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Sample> query = criteriaBuilder.createQuery(Sample.class);
MetaModel m = em.getMetaModel();
EntityType<Sample> entity = m.entity(Sample.class);
Root model = query.from(Sample.class);
query.where(criteriaBuilder.notEqual(model.get(entity.name), “”)).and(criteriaBuilder.notEqual(model.get(entity.name), null));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With