If you actually need to check if the enumeration value exists and still use String for representing status in your search criteria, you have multiple options.
You can for example use the method isValidEnum in the EnumUtils class from the commons-lang library:
if(EnumUtils.isValidEnum(OnboardingTaskStatus.class, params.getStatus())){ predicates.add( cb.equal( root.get("status"), OnboardingTaskStatus.valueOf(params.getStatus()) ) ); }
You can use the getIfPresent method from Guava's Enums class:
Optional<OnboardingTaskStatus> optStatus = Enums.getIfPresent(OnboardingTaskStatus.class, params.getStatus()); if(optStatus.isPresent()){ predicates.add( cb.equal( root.get("status"), OnboardingTaskStatus.valueOf(optStatus.get()) ) ); }
This other SO question provides several alternatives from Jon Skeet and others.
With Java 8, you have several options as well. In addition to the ones provided in other answers, you can try for example:
Optional<OnboardingTaskStatus> optStatus = EnumSet.allOf(OnboardingTaskStatus.class) .stream() .filter(e -> e.name().equals(params.getStatus())) .findAny(); if(optStatus.isPresent()){ predicates.add( cb.equal( root.get("status"), OnboardingTaskStatus.valueOf(optStatus.get()) ) ); }