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("SELECT COUNT(p) FROM PersonEntity p " );
query.getSingleResult().getClass().getCanonicalName() --> java.lang.Long

Native Query

Query query = em.createNativeQuery("SELECT COUNT(*) FROM PERSON");
query.getSingleResult().getClass().getCanonicalName() --> java.math.BigInteger

If it is Native Query big Integer is returned or if it is JPQL then Long is returned.

So before assigning to Integer the value should be typecasted.

 int count = ((Number)arrCount.get[0]).intValue();
 System.out.println(count);

Needs to be Removed

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

	<context:annotation-config />
	<context:component-scan base-package="" />
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	
	
	<!-- Configure the entity manager factory bean -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
		<!-- Set JPA properties -->
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10g</prop>
			</props>
		</property>
		<!-- Set base package of your entities -->
		<property name="packagesToScan" value="com.mugil.org.model" />
	</bean>
	
	
	<beans:bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" lazy-init="true">
    	<beans:property name="jndiName" value="java:jboss/datasources/TurboDS"/>
	</beans:bean>	
</beans>

SpringMVC

Posted in JPA.

Comments are closed.