IN clause with a composite primary key in JPA criteria

前端 未结 3 597
离开以前
离开以前 2021-01-05 09:13

I have a table named group_table in MySQL with only two columns user_group_id and group_id (both of them are of type VARCHAR

3条回答
  •  灰色年华
    2021-01-05 09:36

    You can use a field concatenation approach to solve the problem.

    Create a method that returns the two fields you want to search in your DTO/Entity.

      public String getField1Field2Concatenated() {
        return field1+ field2;
      }
    
    
    List ids = list.stream().map(r -> r.getField1Field2Concatenated()).collect(Collectors.toList());
    

    You can concatenate two fields and do the search.

    Select e from Entity e where concat(e.field1,  c.field2) in (:ids)
    

    If any of the fields are not text you can cast

    Select e from Entity e where concat(cast(c.field1 as string),  c.field2) in (:ids)
    

提交回复
热议问题