简体   繁体   English

JPA Criteria API在带有构造函数表达式的字符串Lob上选择不同的对象

[英]JPA Criteria API select distinct on String Lob with Constructor Expression

I have a static method that creates a criteria query: 我有一个创建条件查询的静态方法:

public static CriteriaQuery<ReportInfo> reportInfoQuery(EntityManager em){
    List<Predicate> criteria = new ArrayList<Predicate>();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ReportInfo> c = cb.createQuery(ReportInfo.class);
    Root<Flaw> flaw = c.from(Flaw.class);
    c.distinct(true);

    c.multiselect(flaw.get("hostinfo").get("name"), flaw.get("severity"),
            flaw.get("plugin").get("pluginid"),
            flaw.get("port"), flaw.get("pluginName"),
            flaw.get("report").get("scan").get("scanDate"),
            flaw.get("text"));

    criteria.add(cb.equal(flaw.get("plugin").get("pluginid"), 0);
    criteria.add(cb.equal(flaw.get("hostinfo").get("name"), "ahostname");

    if (reports.size() > 1 && !reports.contains(null)) {
        List<Predicate> orCriteria = new ArrayList<Predicate>();
        for (String report : reports) {
            orCriteria.add(cb.equal(flaw.get("report").get("name"), report));
        }
        criteria.add(cb.or(orCriteria.toArray(new Predicate[0])));
    } else if (reports.size() == 1 && !reports.contains(null)) {
        criteria.add(cb.equal(flaw.get("report").get("name"), reports.get(0)));
    }

    if (criteria.size() == 1) {
        c.where(criteria.get(0));
    } else if (criteria.size() > 1) {
        c.where(cb.and(criteria.toArray(new Predicate[0])));
    }

    return c;
}

ReportInfo is a constructer expression class, which holds values selected in multiselect. ReportInfo是一个构造器表达式类,其中包含在multiselect中选择的值。

This all works fine with c.distinct(false) , if I use c.distinct(true) I get an execption, because the value flaw.text is a String annotated with @Lob (large texts) 如果我使用c.distinct(true) c.distinct(false) ,这一切都可以与c.distinct(false)一起c.distinct(false)工作,因为值瑕疵.text是一个用@Lob注释的字符串(大文本),因此我得到了执行。

Has anyone an Idea, how I can solve that problem? 有谁知道,我该如何解决这个问题? We use Eclipse Link 2. 我们使用Eclipse Link 2。

Your database does not support the distinct operation on a LOB. 您的数据库不支持LOB上的不同操作。 You need to either remove the distinct, or remove the Lob from the select. 您需要删除唯一标识符,或从选择中删除Lob。 Not sure why you require a distinct, you are not doing any joins, so should not get any duplicate rows. 不知道为什么需要一个独立的,没有进行任何联接,因此不应获取任何重复的行。

If you are getting duplicates you could try to filter them in a sub-select, or define an equals method in your ReportInfo class and add your results to a list to remove the duplicates. 如果要获取重复项,则可以尝试在子选择中对其进行过滤,或者在ReportInfo类中定义一个equals方法,然后将结果添加到列表中以删除重复项。

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

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