You do not need to "distinguish" the value at all, in fact you just need to pass the values ββin the form that they store.
If we assume that your field was annotated only as @Enumerated(EnumType.STRING) , the column will be a simple varchar field. (Matching java type with postgres enumeration is another big topic.)
If now you want to compare your list of Status enum instances with the associated string values ββin db, pass it as a collection of strings, in other words, call it toString() if it is java enum .
eg. this was your listing:
public enum EntityStatus { A, B, C; }
That was your essence:
import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.hibernate.annotations.Filter; import org.hibernate.annotations.FilterDef; import org.hibernate.annotations.ParamDef; @Entity @FilterDef(name = "byMultipleStates", defaultCondition = "status in (:states)", parameters = @ParamDef(name = "states", type = "string")) @Filter(name = "byMultipleStates", condition = "status in (:states)") public class StatusEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Enumerated(EnumType.STRING) private EntityStatus status; public long getId() { return id; } public EntityStatus getStatus() { return status; } public void setStatus(EntityStatus status) { this.status = status; } }
This could be your filtering code:
public List<StatusEntity> filterByStates(final Set<EntityStatus> states) { final Session hibernateSession = entityManager.unwrap(Session.class); hibernateSession.enableFilter("byMultipleStates").setParameterList("states", states.stream().map(state -> state.toString()).collect(Collectors.toList())); final Query query = hibernateSession.createQuery("SELECT e FROM StatusEntity e"); return query.list(); }
Or on the way to Java 8:
public List<StatusEntity> filterByStates(final Set<EntityStatus> states) { final Set<String> statesAsString = new HashSet<>(); for (final EntityStatus state : states) { statesAsString.add(state.toString()); } final Session hibernateSession = entityManager.unwrap(Session.class); hibernateSession.enableFilter("byMultipleStates").setParameterList("states", statesAsString); final Query query = hibernateSession.createQuery("SELECT e FROM StatusEntity e"); return query.list(); }
Thus, only filtering is possible for a set of values.