View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.jpa;
17  
18  import javax.persistence.EntityManager;
19  import javax.persistence.Query;
20  import javax.persistence.TypedQuery;
21  import java.util.List;
22  
23  /**
24   * JPA specialization of DataObjectCriteriaQuery.
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  class JpaCriteriaQuery extends DataObjectCriteriaQueryBase<NativeJpaQueryTranslator.TranslationContext, TypedQuery> {
29  
30      /**
31       * The query translator to use for this implementation.
32       */
33      protected QueryTranslator<NativeJpaQueryTranslator.TranslationContext, TypedQuery> queryTranslator;
34  
35      /**
36       * Creates a new JPA-specific criteria query.
37       *
38       * @param em the entity manager used in interacting with the database.
39       */
40      public JpaCriteriaQuery(EntityManager em) {
41          this.queryTranslator = new NativeJpaQueryTranslator(em);
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      protected QueryTranslator<NativeJpaQueryTranslator.TranslationContext, TypedQuery> getQueryTranslator() {
49          return queryTranslator;
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      @SuppressWarnings("unchecked")
57      public <T> List<T> getResults(TypedQuery query) {
58          return query.getResultList();
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public int executeUpdate(Query query) {
66          return query.executeUpdate();
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      protected int getRowCount(TypedQuery query) {
74          // NOTE: is there a better way to do this (e.g. without retrieving full results)?
75          return query.getResultList().size();
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public int getIncludedRowCount(TypedQuery query, List rows) {
83          return rows.size();
84      }
85  }