对于这个问题,您可以在查询时通过join()方法指定不查询@OneToMany关联表。
举个例子,如果有一个实体A与实体B是一对多关系,那么您可以这样写查询条件:
@OneToMany(targetEntity = DcyRightImage.class,cascade = {CascadeType.ALL},fetch = FetchType.EAGER) @JoinColumn(name = "geo_id", referencedColumnName = "id") private Set<DcyRightImage> images; @OneToMany(targetEntity = DcyRightVideo.class,cascade = {CascadeType.ALL},fetch = FetchType.EAGER) @JoinColumn(name = "geo_id", referencedColumnName = "id") private Set<DcyRightVideo> videos;
public static Specification webRightConditionSpecification(String code, String name, String land, String remark1, Date createdTime, String releaseStatus, String geo) { Specification<Object> specification = (Specification<Object>) (root, query, cb) -> { List<Predicate> predicates = new ArrayList<>(); predicates.add(cb.equal(root.get("deleted"), 0)); //通过JoinType方法 指定不查询@OneToMany关联表, root.fetch( "images", JoinType.LEFT); //指定不查询关联表 ,注意,此关键词为实体类中的表属性名称 root.fetch( "videos", JoinType.LEFT); ////指定不查询关联表 ,注意,此关键词为实体类中的表属性名称 // 查询已经发布的桥梁信息 if (geo != null) { LiteralExpression<String> regitsterAdressExp = new LiteralExpression<>(null, geo); // 函数名称,返回值类型ST_Intersects(geo,table.gep) Expression<Boolean> similarityFunc = cb.function("ST_Intersects",Boolean.class,regitsterAdressExp, root.get("geo")); predicates.add(cb.isTrue(similarityFunc)); } if (createdTime != null) { predicates.add(cb.greaterThan(root.get("createdTime"), createdTime)); } if (StringUtils.isNoEmpty(remark1)) { predicates.add(cb.or(cb.like(root.get("remark1").as(String.class),"%"+ remark1 + "%"))); } if (StringUtils.isNoEmpty(releaseStatus)) { predicates.add(cb.equal(root.get("releaseStatus"), PublicParam.HAVE_RELEASE)); } if (StringUtils.isNoEmpty(code)) { predicates.add(cb.or(cb.like(root.get("code").as(String.class),"%"+ code + "%"))); } if (StringUtils.isNoEmpty(name)) { predicates.add(cb.or(cb.like(root.get("rightobligee").as(String.class),"%"+ name + "%"))); predicates.add(cb.or(cb.like(root.get("rightcardno").as(String.class),"%"+ name + "%"))); } if (StringUtils.isNoEmpty(land)) { predicates.add(cb.or(cb.like(root.get("land").as(String.class),"%"+ land + "%"))); } return cb.and(predicates.toArray(new Predicate[0])); }; return specification; }
- OneToMany JpaSpecificationExecutor Specification 实体 时候onetomany jpaspecificationexecutor specification onetomany jpaspecificationexecutor jpaspecificationexecutor jparepository接口 specification ux-specification modelspecification specification required error specification案例jpa 39 parseexception specification destination specification ardalis模式