JPA CriteriaQuery:getCount + condition

时间:2013-09-18 07:42:49

标签: jpa count predicate criteriaquery

我试图得到所有未分析的文章(Article.class)(分析== false)。 遗憾的是,以下代码返回绝对错误的数字。 有谁知道为什么?

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> query = cb.createQuery(Long.class);
query.select(cb.count(query.from(Article.class)));
Root<Article> articles = query.from(Article.class);
Predicate condition = cb.isFalse(articles.get(Article_.analyzed));
query.where(condition);
TypedQuery<Long> unanalyzedArticlesAmount = entityManager.createQuery(query);
return unanalyzedArticlesAmount.getSingleResult();

1 个答案:

答案 0 :(得分:0)

终于阅读了这篇文章: How to count the number of rows of a JPA 2 CriteriaQuery in a generic JPA DAO?

并找到以下解决方案:

       DECLARE @p__linq__0 bigint = 131074
       ,@p__linq__1 datetimeoffset(7) = '2017-10-26 00:00:00 +00:00'
       ,@p__linq__2 datetimeoffset(7) = '2016-10-26 00:00:00 +00:00'
SELECT  distinct
            1 AS [C1],        
            [Extent5].[DisplayText] + ' (' + [Extent4].[ParameterCode] + ')' AS [C2],
            [Extent4].[ParameterCode] AS [ParameterCode], 
            [Extent5].[ParameterId] AS [ParameterId] 

--[Extent2].[ReceptionDate] AS [ReceptionDate], [Extent4].[ParameterId] AS [ParameterId], [Extent4].[ParameterCode] AS [ParameterCode]
                FROM    [dbo].[ClientSamples] AS [Extent1]
                INNER JOIN [dbo].[SamplesView] AS [Extent2] ON [Extent1].[SampleId] = [Extent2].[SampleId] 
                and [Extent1].[AccountCode] IN ('A00052498', 'A00091603', 'AFR000790', 'AFR025580', 'AFR033702', 'AFR034669', 'AFR065301')  
                AND [Extent2].[ReceptionDate] >= @p__linq__2 AND [Extent2].[ReceptionDate] <= @p__linq__1 
                INNER JOIN [dbo].[SampleTestFractionsView] AS [Extent3] ON [Extent2].[SampleId] = [Extent3].[SampleId]
                INNER JOIN [dbo].[ValidatedResults] AS [Extent4] ON [Extent3].[SampleTestFractionId] = [Extent4].[SampleTestFractionId]
                INNER JOIN [dbo].[ParameterTranslationsView] AS [Extent5] ON [Extent4].[ParameterId] = [Extent5].[ParameterId] AND (@p__linq__0 = [Extent5].[LanguageId])
                WHERE ([Extent5].[DisplayText] + ' (' + [Extent4].[ParameterCode] + ')' IS NOT NULL OR 
                (CAST(LEN([Extent5].[DisplayText] + ' (' + [Extent4].[ParameterCode] + ')') AS int)) != 0) 
                AND ' ' <> ([Extent5].[DisplayText] + ' (' + [Extent4].[ParameterCode] + ')')