我试图获取一个实体列表及其基于最深层字段值的关系。本机sql查询本身似乎足够简单,但我无法使用jpql或规范进行过滤。我的table的结构是 Ad -> SubmittedValue -> Configuration
,我希望能够按 Configuration
调用的实体 isSpecial
. 我目前有:
@Entity
public class Ad extends BaseEntity{
@JoinColumn(name = "user_id")
private User user;
@OneToMany(mappedBy = "ad")
private List<SubmittedValue> values;
}
@Entity
/* @ToString and @EqualAndHashCode exclude "ad" */
public class SubmittedValue extends BaseEntity{
@ManyToOne
@JoinColumn(name = "ad_id")
private Ad ad;
@ManyToOne
@JoinColumn(name = "configuration_id")
private Configuration configuration;
}
@Entity
public class Configuration extends BaseEntity{
private Boolean isSpecial = false;
}
到目前为止,我已经尝试使用jpql:
@Query("select a from Ad a inner join a.values as v inner join v.configuration as c on c.isSpecial = true where a.user.id =?1")
Page<Ad> findAllBySpecialConfiguration(Pageable page, Long userId);
以及下面写的说明。它们都正确地返回了实体,但排除了 SubmittedValues
其中 isSpecial
的价值 configuration
是 false
未应用于结果。以下是我的说明:
public static Specification<Ad> adSpecial(Long userId) {
return (root, query, criteriaBuilder) -> {
ListJoin<Ad, SubmittedValue> values = root.join(Ad_.values);
Join<SubmittedValue, Configuration> config = values.join(SubmittedValue_.configuration);
// config.on(criteriaBuilder.isTrue(config.get(Configuration_.isSpecial).as(Boolean.class)));
List<Predicate> conditions = new ArrayList<>();
conditions.add(criteriaBuilder.isTrue(config.get(Configuration_.isSpecial).as(Boolean.class)));
conditions.add(criteriaBuilder.equal(root.get(Ad_.user).get(User_.id).as(Long.class), userId));
return criteriaBuilder.and(conditions.toArray(new Predicate[0]));
};
}
谢谢和问候!
1条答案
按热度按时间kmynzznz1#
你必须改变你的产品规格
到
通过将isspecial作为如下参数值,可以使您的规范更加动态