繁体   English   中英

创建 Spring 数据 Jpa 规范方法没有JOINS

[英]Create Spring Data Jpa Specification method without JOINS

我有一个Spring-MVC在线商店项目,我使用Spring BootHibernate 我决定使用Specification进行过滤 因此,我使用JOINS为规范编写了一个方法。 请告诉我如何在没有JOIN的情况下编写相同的方法。

茶叶规格 class:

public static Specification<Tea> getTeasByFilter(Long colorId, Long typeId, Long countryId) {
        return (root, query, criteriaBuilder) -> {
            Join<Object, Object> colorJoin = root.join(Tea_.TEA_COLOR);
            Join<Object, Object> typeJoin = root.join(Tea_.TEA_TYPE);
            Join<Object, Object> countryJoin = root.join(Tea_.COUNTRIES);
            Predicate countryPredicate = criteriaBuilder.equal(countryJoin.get(Countries_.ID), countryId);
            Predicate colorPredicate = criteriaBuilder.equal(colorJoin.get(TeaColor_.ID), colorId);
            Predicate typePredicate = criteriaBuilder.equal(typeJoin.get(TeaColor_.ID), typeId);
            return criteriaBuilder.and(colorPredicate, typePredicate, countryPredicate);
        };
    }

饮料类(茶扩展饮料):

@Inheritance(strategy = InheritanceType.JOINED)
public class Drink {

    // Fields
    //
    private @Id
    @GeneratedValue
    Long id;

    private String name;

    private BigDecimal price;

    private String about;

    @Column(name = "is_deleted")
    private boolean isDeleted;

    // Relationships
    //
    @ManyToOne
    @JoinColumn(name = "packaging_id")
    private Packaging packaging;

    @ManyToOne
    @JoinColumn(name = "manufacturer_id")
    private Manufacturer manufacturer;

    @ManyToOne
    @JoinColumn(name = "country_id")
    private Countries countries;
}
public class Tea extends Drink {

    // Relationships
    //
    @ManyToOne
    @JoinColumn(name = "type_id")
    private TeaType teaType;

    @ManyToOne
    @JoinColumn(name = "color_id")
    private TeaColor teaColor;
}
public class TeaSpecification {

    public static Specification<Tea> getTeasByFilter(Long colorId, Long typeId, Long countryId) {
        return (root, query, criteriaBuilder) -> {
            Predicate colorPredicate = criteriaBuilder
                    .equal(root.get(Tea_.teaColor).get(TeaColor_.id), colorId);
            Predicate typePredicate = criteriaBuilder
                    .equal(root.get(Tea_.teaType).get(TeaType_.id), typeId);
            Predicate countryPredicate = criteriaBuilder
                    .equal(root.get(Tea_.countries).get(Countries_.id), countryId);
            return criteriaBuilder.and(colorPredicate, typePredicate, countryPredicate);
        };
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM