{"id":2360,"date":"2017-06-02T08:27:46","date_gmt":"2017-06-02T08:27:46","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2360"},"modified":"2017-06-27T16:26:42","modified_gmt":"2017-06-27T16:26:42","slug":"getting-count-in-jpa","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/getting-count-in-jpa\/","title":{"rendered":"Getting Count in JPA"},"content":{"rendered":"<p><strong>JPA COUNT returns a Long<\/strong><br \/>\nWhen extracting the count from the table JPA returns long based on the type of query used.<\/p>\n<p><strong>JPQL<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nQuery query = em.createQuery(&quot;SELECT COUNT(p) FROM PersonEntity p &quot; );\r\nquery.getSingleResult().getClass().getCanonicalName() --&gt; java.lang.Long\r\n<\/pre>\n<p><strong>Native Query<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nQuery query = em.createNativeQuery(&quot;SELECT COUNT(*) FROM PERSON&quot;);\r\nquery.getSingleResult().getClass().getCanonicalName() --&gt; java.math.BigInteger\r\n<\/pre>\n<p>If it is Native Query big Integer is returned or if it is JPQL then Long is returned.<\/p>\n<p>So before assigning to Integer the value should be typecasted.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n int count = ((Number)arrCount.get&#x5B;0]).intValue();\r\n System.out.println(count);\r\n<\/pre>\n<p><em>Needs to be Removed<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n\txmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot; xmlns:jpa=&quot;http:\/\/www.springframework.org\/schema\/data\/jpa&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\r\n   http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\r\n   http:\/\/www.springframework.org\/schema\/context\r\n   http:\/\/www.springframework.org\/schema\/tx\r\n   http:\/\/www.springframework.org\/schema\/data\/jpa http:\/\/www.springframework.org\/schema\/data\/jpa\/spring-jpa.xsd&quot;&gt;\r\n\r\n\t&lt;context:annotation-config \/&gt;\r\n\t&lt;context:component-scan base-package=&quot;&quot; \/&gt;\r\n\t&lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot;\/&gt;\r\n\t\r\n\t\r\n\t&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;\r\n\t\t&lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot; \/&gt;\r\n\t&lt;\/bean&gt;\r\n\t\r\n\t\r\n\t&lt;!-- Configure the entity manager factory bean --&gt;\r\n\t&lt;bean id=&quot;entityManagerFactory&quot;\r\n\t\tclass=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;\r\n\t\t&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; \/&gt;\r\n\t\t&lt;property name=&quot;jpaVendorAdapter&quot; ref=&quot;hibernateJpaVendorAdapter&quot; \/&gt;\r\n\t\t&lt;!-- Set JPA properties --&gt;\r\n\t\t&lt;property name=&quot;jpaProperties&quot;&gt;\r\n\t\t\t&lt;props&gt;\r\n\t\t\t\t&lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.Oracle10g&lt;\/prop&gt;\r\n\t\t\t&lt;\/props&gt;\r\n\t\t&lt;\/property&gt;\r\n\t\t&lt;!-- Set base package of your entities --&gt;\r\n\t\t&lt;property name=&quot;packagesToScan&quot; value=&quot;com.mugil.org.model&quot; \/&gt;\r\n\t&lt;\/bean&gt;\r\n\t\r\n\t\r\n\t&lt;beans:bean id=&quot;dbDataSource&quot; class=&quot;org.springframework.jndi.JndiObjectFactoryBean&quot; scope=&quot;singleton&quot; lazy-init=&quot;true&quot;&gt;\r\n    \t&lt;beans:property name=&quot;jndiName&quot; value=&quot;java:jboss\/datasources\/TurboDS&quot;\/&gt;\r\n\t&lt;\/beans:bean&gt;\t\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><a href=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2017\/06\/SpringMVC.zip\">SpringMVC<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JPA COUNT returns a Long When extracting the count from the table JPA returns long based on the type of query used. JPQL Query query = em.createQuery(&quot;SELECT COUNT(p) FROM PersonEntity p &quot; ); query.getSingleResult().getClass().getCanonicalName() &#8211;&gt; java.lang.Long Native Query Query query = em.createNativeQuery(&quot;SELECT COUNT(*) FROM PERSON&quot;); query.getSingleResult().getClass().getCanonicalName() &#8211;&gt; java.math.BigInteger If it is Native Query big Integer&hellip; <a href=\"https:\/\/codethataint.com\/blog\/getting-count-in-jpa\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[235],"tags":[],"class_list":["post-2360","post","type-post","status-publish","format-standard","hentry","category-jpa"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2360"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2360\/revisions"}],"predecessor-version":[{"id":2384,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2360\/revisions\/2384"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}