规范<T>用于筛选具有关系@ManyToOne的联接表

Specification<T> used for filtering join table with relationship @ManyToOne

提问人:Michele Genchi 提问时间:7/7/2023 最后编辑:Michele Genchi 更新时间:7/10/2023 访问量:25

问:

!(https://i.stack.imgur.com/p5Tjt.png)

    {
        "country_code":["IT", "FR", "EG"]
    }

通过这种方式,我可以过滤所有城市的特定country_code 这里模型城市和国家

    package comgenchi.geotools.model;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    import javax.validation.constraints.NotBlank;
    import javax.validation.constraints.Pattern;

    import org.hibernate.validator.constraints.Range;
    import org.springframework.validation.annotation.Validated;

    import lombok.Data;

    @Data
    @Entity
    @Table(name="countries")
    @Validated
    public class City {
        
        @Id
        @GeneratedValue
        protected int Id;

        @ManyToOne
        @JoinColumn(name="country_code")
        protected Country country;

        protected String postal_code;
        
        @NotBlank(message="position can not be blank")
        protected String position;

        @NotBlank(message="region can not be blank")
        protected String region;

        @NotBlank(message="region_code can not be blank")
        protected String region_code;

        @NotBlank(message="province can not be blank")
        protected String province;
        
        @Pattern(regexp="^([A-Z]{2,3})|([/d]{0,5})|(^$)$", message="sigle province format error")
        protected String sigle_province;

        @Range(min=-90, max=90, message="Range for latitude must be from -90 to 90")
        protected String latitude;

        @Range(min=-180, max=180, message="Range for longitude must be from -90 to 90")
        protected String longitude;

    }

    package comgenchi.geotools.model;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.validation.constraints.NotBlank;
    import javax.validation.constraints.Pattern;

    import org.springframework.validation.annotation.Validated;

    import lombok.Data;

    @Data
    @Entity
    @Table(name="countries")
    @Validated
    public class Country {

        @Id
        @Column(name="country_code", unique=true, nullable=false)
        @Pattern(regexp="^[A-Za-z]{2}$", message="country_code must be have 2 letters")
        @NotBlank(message = "country_code not blank")
        protected String country_code;

        @Column(name="country", unique=true, nullable=false)
        @NotBlank(message = "country name not blank")
        protected String name;

    }

此类实现规范 解析联合表城市/国家/地区 这样,我就可以在 Array 有效负载中拥有>每个国家/地区的所有城市 {“country_code”: [“意大利”, “法语”, “EG”]} 这是我的 Json Response, bad e good JsonResponse

    package comgenchi.geotools.controller;

    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Path;
    import javax.persistence.criteria.Predicate;
    import javax.persistence.criteria.Root;

    import org.springframework.data.jpa.domain.Specification;
    import org.springframework.lang.Nullable;

    import comgenchi.geotools.model.SearchCriteria;

    // return in all method un specification<Country>
    public final class Specs<T> implements Specification<T> {

      private final SearchCriteria criteria;

      public Specs(SearchCriteria searchCriteria) {
        this.criteria = searchCriteria;
      }

      @Override
      @Nullable
      public Predicate toPredicate(
        Root<T> root,
        CriteriaQuery<?> query,
        CriteriaBuilder builder
      ) {
        
        Path<Object> key = null;
        //here resolve join Join Country/City
> 

        switch (criteria.getOperation()) {
          case ":":
            if (key.getJavaType() == String.class) {
              return builder.like(
                root.<String>get(criteria.getKey()),
                "%" + criteria.getValue() + "%"
              );
            } else {
              return builder.equal(key, criteria.getValue());
            }
          case "=":
            return builder.equal(key, criteria.getValue());
          case "in":
            return builder.in(key).value(criteria.getValue());
          default:
            return null;
        }
      }
    }

Java Spring Join 规格 多对一

评论

0赞 Grim 7/8/2023
嘿,我没有收到你的问题。
0赞 Michele Genchi 7/9/2023
此类在 criteria.getKey().contains(“.”))所以过滤器 将在联合表城市/国家/地区工作 这样,我就可以在数组有效负载 {“country_code”: [“IT”, “FR”]} 中拥有>每个国家/地区的所有城市
0赞 Grim 7/9/2023
嗯,这是毫无疑问的。您是否知道我们希望有一个问题如何解决问题?
0赞 Michele Genchi 7/10/2023
这是关于我的回复的例子,糟糕的回应和良好的回应链接在这里

答:

0赞 Michele Genchi 7/10/2023 #1
enter code here    
@Data
@Entity
@Table(name="countries") //table is cities (this is a problem)
@Validated
public class City {

我的代码没问题