休眠:查询计数查询时从堆栈溢出错误(从表中选择count(*))

时间:2019-08-19 05:51:42

标签: java hibernate spring-boot spring-data-jpa hibernate-criteria

Hibernate(Hibernate版本5.0.11)条件计数查询引发stackoverflowerror:null错误,当有更多记录(如1000条以上)时出现,但在记录受到限制时则看不到。

StackTrace:

The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container 
java.lang.StackOverflowError: null
    at antlr.CharScanner.consume(CharScanner.java:80) ~[antlr-2.7.7.jar!/:na]
    at antlr.CharScanner.match(CharScanner.java:197) ~[antlr-2.7.7.jar!/:na]
    at org.hibernate.hql.internal.antlr.HqlBaseLexer.mNUM_INT(HqlBaseLexer.java:793) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseLexer.nextToken(HqlBaseLexer.java:242) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at antlr.TokenBuffer.fill(TokenBuffer.java:69) ~[antlr-2.7.7.jar!/:na]
    at antlr.TokenBuffer.LA(TokenBuffer.java:80) ~[antlr-2.7.7.jar!/:na]
    at antlr.LLkParser.LA(LLkParser.java:52) ~[antlr-2.7.7.jar!/:na]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:964) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3530) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3383) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3255) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2912) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:597) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2679) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2540) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2504) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2420) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2385) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2098) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.expressionOrVector(HqlBaseParser.java:4664) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:938) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3530) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3383) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3255) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
    at org.hibernate.hql.internal.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2912) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]

代码:

CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
Root<T> root = criteria.from(myclass.class);
criteria.select(builder.count(root)); 
getEntityManager().createQuery(criteria).getSingleResult().intValue();

0 个答案:

没有答案