4

I need to write dynamic query with various number of parameters in where clause. I want to use specification object as it supports dynamic queries.

I have following entity in my system:

@Getter
@Setter
@Entity
@Table(name = "thing")
public class ThingEntity implements Serializable {

  @Id private String id;

  private String type;

  @ElementCollection
  @JoinTable(name = "thing_metadata", joinColumns = @JoinColumn(name = "thing_id"))
  @MapKeyColumn(name = "key")
  @Column(name = "value")
  private Map<String, String> metadata;

  ...  
}

Query that I need to execute is following:

select id, type
from thing
where id not in (
                select thing_id
                from thing_metadata
                where key = 'paramName1' or key ='paramName2'
)

Can anyone help me to write query using criteria builder?

public Specification<ThingEntity> specificationByKeys() {
  return (root, query, cb) -> {
     ???
  };
}
Adam
  • 873
  • 7
  • 33
mchrobok
  • 1,947
  • 2
  • 20
  • 22
  • Not exactly, it is not a dupliate. As you can see `metadata` is type of `Map`. When I use solutions mentioned in two links above I get error `Not an entity: interface java.util.Map`. It works only if `metadata` would be an entity. – mchrobok Nov 21 '19 at 09:10

0 Answers0