jpa使用条件构建器选择嵌入的elementcollection查询

时间:2017-05-06 12:48:31

标签: hibernate jpa criteria predicate

我有一个带有嵌入式元素集合的实体,如下所示:

@Configuration
public class SpringBootMetrics {

    @Bean
    @ExportMetricReader
    public MetricReader metricReader() {
        return new MetricRegistryMetricReader(metricRegistry());
    }   

    public MetricRegistry metricRegistry() {
        final MetricRegistry metricRegistry = new MetricRegistry();
        return metricRegistry;
    }

    @Bean
    @ExportMetricWriter
    MetricWriter metricWriter(MBeanExporter exporter) {
        return new JmxMetricWriter(exporter);
    }
}

我想使用JPA CriteriaBuilder框架编写一个Criteria,这样当我传递... @Embedded @ElementCollection(targetClass = ProviderResponse.class) @JoinTable( name = "provider_response", joinColumns = @JoinColumn(name = "id") ) @AttributeOverrides({ @AttributeOverride(name = "provider", column = @Column(name = "provider")), @AttributeOverride(name = "response", column = @Column(name = "response")) }) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public Set<ProviderResponse> getProviderResponses() { return providerResponses; } ... 进行比较时,它会返回ProviderResponse ProviderResponse中所有providerResponses的实体。元素集合。

ProviderResponse只是两个枚举类ProviderResponse的复合类。

我试图创建谓词:

Predicate providerResponse = builder.equal(root.<Set<ProviderResponse>>get("providerResponses"), compProviderResponse);

这不是说工作 &#34;参数值ProviderResonse与预期类型不匹配[java.util.Set&#34;

我理解这一点,但我不确定如何进行我想要的比较。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

多么尴尬,我只是反思了一下,找到了答案。

谓词应该是:

Predicate providerResponsePredicate = builder.isMember(compProviderResponse, root.<Set<ProviderResponse>>get("providerResponses"));