使用子查询的最小值和最大值

时间:2018-02-13 16:54:00

标签: java criteria

我的table1有4列(Column1,Column2,e,table2ID(这是一个ManyToOne))e和table2ID的值是我的方法的输入我试图检索对应的值:值来自table1的e + table2ID(已接收)+ max(Column1)+ min(Column2)这是我得到的错误:

  
    

org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:)靠近第1行,第483行[从table1选择generatedAlias0作为generatedAlias0,table1作为generatedAlias1,其中(generatedAlias0.table2ID.table2ID = 10200L)和(generatedAlias0。 e = 1L)和(generatedAlias0.isEnd = false)和(     generatedAlias0.Column1 in(从table1中选择max(generatedAlias2.Column1)as generatedAlias2))和(generatedAlias0.Column2in(select min(generatedAlias1.Column2)from)))

  

这是我的代码:

public table1 find(final Long table2ID, final Long e) {

    final CriteriaBuilder builder = getCriteriaBuilder();
    final CriteriaQuery<table1> criteriaQuery = builder.createQuery(table1.class);
    final Root<table1> from = criteriaQuery.from(table1.class);
    final Predicate p1 = builder.equal(from.get(table1_.table2ID).get(table2_.table2ID), table2ID);
    final Predicate p2 = builder.equal(from.get(table1_.e), e);
    final Predicate p3 = builder.equal(from.get(table1_.isEnd), false);

    final Subquery<Long> sq1 = criteriaQuery.subquery(Long.class);
    final Root<table1> maxColumn1 = sq1.from(table1.class);
    sq1.select(builder.max(maxColumn1 .get(table1_.Column1)));

    final Predicate p4 = builder.in(from.get(table1_.Column1)).value(sq1);

    final Subquery<Long> sq2 = criteriaQuery.subquery(Long.class);
    final Root<table1> minColumn2 = criteriaQuery.from(table1.class);
    sq2.select(builder.min(minColumn2 .get(table1_.Column2)));

    final Predicate p5 = builder.in(from.get(table1_.Column2) ).value(sq2);

    criteriaQuery.select(from).where(builder.and(p1, p2, p3,p4,p5));

    final List<table1> liste= listByCriteriaQuery(criteriaQuery);
    if (!liste.isEmpty()) {
        return liste.get(0);
    }

    return null;
}

请告诉我子查询的形式有什么问题 PS:我是初学者

0 个答案:

没有答案