java - Hibernate @Filter collection of enums -


i need figure out how apply annotation-based filtering parameter list of enums defined as:

@column(name = "target_status") @enumerated(enumtype.string) @type(type="pgenumconverter", parameters = {     @parameter(name = "enumclassname",                value = "com.company.model.campaign.campaigntarget$status") }) private status targetstatus; 

so @filterdef looks this:

    @filterdef(name="filtercampaigntargetbystatuses",                defaultcondition="target_status in (:statuses)",                parameters = @paramdef(name = "statuses", type = "string")) 

and when enable filter looks this:

    session.enablefilter("filtercampaigntargetbystatuses").     setparameterlist("statuses", statuses); 

and error hibernate is:

 org.hibernate.hibernateexception: incorrect type parameter [statuses] 

the data in postgresql , definition of type:

create type statuscmp enum ('active','inactive','paused','draft','scheduled','ended','archived'); 

i've seen lot of questions how criteria queries , filters against single enum value, none yet filtering on set of enum values. there way explicitly cast individual values?

you don't have "cast" value in general, in fact have pass values in form stored.

if assume field annotated @enumerated(enumtype.string) column plain varchar field. (mapping java type postgres enum big topic.)

if want compare list of status enum instances related string values in db, pass collection of strings, in other words, call it's tostring() method if it's java enum.

e.g. enum:

public enum entitystatus {     a, b, c; } 

this entity:

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 code filter:

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 statusentity e");      return query.list(); } 

or in way before java 8:

public list<statusentity> filterbystates(final set<entitystatus> states) {     final set<string> statesasstring = new hashset<>();     (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 statusentity e");      return query.list(); } 

so filtering collection of values possible.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -