Announcement Announcement Module
Collapse
No announcement yet.
ClassCastException using Oracle EntityManager and JPA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException using Oracle EntityManager and JPA

    Hello all,

    I am trying to get the entity manager working with spring batch. In the existing spring batch framework, I have implemented the following:

    My persistence.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	
    		xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    
    	<persistence-unit name="issue-pu" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    
    		<class>mil.dfas.tsopr.nimms.db.NimmsTest</class>
    		<properties>
    			<!-- property name="toplink.logging.level" value="OFF"/> -->
    			<!-- property name="toplink.logging.timestamp" value="false"/> -->
    			<!-- property name="toplink.logging.thread" value="false"/> -->
    			<!-- property name="toplink.logging.session" value="false"/> -->
    			<!-- property name="toplink.throw.orm.exceptions" value="true"/> -->
    	
    			<!-- property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/> -->
    			<!-- property name="hibernate.hbm2ddl.auto" value="create-drop"/> -->
    			<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
    			<property name="hibernate.archive.autodetection" value="class"/>
    		</properties>
    	</persistence-unit>
    </persistence>
    Im my simple-job-launcher-context.xml I have the following:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context" 
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:p="http://www.springframework.org/schema/p" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <!-- ================================================== ======================= -->
    <!-- Add Imports here -->
    <!-- ================================================== ======================= -->	
    		
    	<import resource="data-source-context.xml" />
    	<import	resource="classpath:/org/springframework/batch/sample/config/common-context.xml" />
    	
    	
    	
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    	
    
    	<!-- ================================================== ======================= -->
    	<!-- General -->
    	<!-- ================================================== ======================= -->
    	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
    	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
    	
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location">
    			<value>batch-oracle.properties</value>
    		</property>
    	</bean>
    	
    	<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    		<property name="persistenceXmlLocations">
    			<list>
    				<value>META-INF/persistence.xml</value>
    			</list>
    		</property>
    		<property name="loadTimeWeaver">
    			<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
    		</property>
    	</bean>
    	
    	<!-- ================================================== ======================= -->
    	<!-- DataSource -->
    	<!-- ================================================== ======================= -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName">
    			<value>${batch.jdbc.driver}</value>
    		</property>
    		<property name="url">
    			<value>${batch.jdbc.url}</value>
    		</property>
    		<property name="username">
    			<value>${batch.jdbc.user}</value>
    		</property>
    		<property name="password">
    			<value>${batch.jdbc.password}</value>
    		</property>
    	</bean>
    
    	<!-- ================================================== ======================= -->
    	<!-- JPA Persistence Manager Hibernate -->
    	<!-- ================================================== ======================= -->
    
    	<!-- This is the EntityManagerFactory configuration for Hibernate -->
    	<bean id="nimmsEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="persistenceUnitManager" ref="persistenceUnitManager" />
    		<property name="persistenceUnitName" value="issue-pu"/>	
    		<property name="jpaDialect">
            	<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
               </bean>
         	</property>
         	<property name="jpaProperties"> 
    			<props> 
    				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
    				<prop key="hibernate.hbm2ddl.auto">update</prop> 
    			</props> 
    		</property> 
    	</bean>	
    	
    	<bean id="jobLauncher"
    		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    		<property name="jobRepository" ref="jobRepository" />
    	</bean>
    
    	<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    		<property name="jobRegistry" ref="jobRegistry"/>
    	</bean>
    
    	<bean id="jobRepository"
    		class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
    		p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" 
    		p:tablePrefix="NIMMS_OWN.BATCH_"/>
    
    	<!-- <bean id="mapJobRepository"
    		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"
    		lazy-init="true" autowire-candidate="false" /> -->
    
    	<bean id="jobOperator"
    		class="org.springframework.batch.core.launch.support.SimpleJobOperator"
    		p:jobLauncher-ref="jobLauncher" p:jobExplorer-ref="jobExplorer"
    		p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry" />
    
    	<bean id="jobExplorer"
    		class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
    		p:dataSource-ref="dataSource" />
    
    	<bean id="jobRegistry"
    		class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
    
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    	<bean id="logAdvice" class="org.springframework.batch.sample.common.LogAdvice" />
    
    	<bean id="eventAdvice"
    		class="org.springframework.batch.sample.jmx.StepExecutionApplicationEventAdvice" />
    		
    	<!-- ================================================== =======================
    	PostProcessors to perform exception translation on @Repository classes
    	and injection according to the JPA specification.
    	A JPA namespace handler will simplify specification of entity manager factories
    	and remove the requirement for these bean definitions in 2.0 final.
    	================================================== ======================= -->
    	<bean class="org.springframework.dao.annotation.Persiste nceExceptionTranslationPostProcessor"/>
    
    	<context:annotation-config />
    
    	<bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor"/>	
    
    </beans>
    I am getting the following error when I try to connect to my oracle10g database:

    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: issue-pu] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityMan agerFactory(Ejb3Configuration.java:677)
    at org.hibernate.ejb.HibernatePersistence.createConta inerEntityManagerFactory(HibernatePersistence.java :132)
    at org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean.createNativeEntityManagerFactory( LocalContainerEntityManagerFactoryBean.java:224)
    at org.springframework.orm.jpa.AbstractEntityManagerF actoryBean.afterPropertiesSet(AbstractEntityManage rFactoryBean.java:291)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1369)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1335)
    ... 16 more
    Caused by: org.hibernate.HibernateException: Could not instantiate dialect class
    at org.hibernate.dialect.DialectFactory.buildDialect( DialectFactory.java:84)
    at org.hibernate.dialect.DialectFactory.buildDialect( DialectFactory.java:42)
    at org.hibernate.cfg.SettingsFactory.determineDialect (SettingsFactory.java:426)
    at org.hibernate.cfg.SettingsFactory.buildSettings(Se ttingsFactory.java:128)
    at org.hibernate.cfg.Configuration.buildSettings(Conf iguration.java:2073)
    at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1298)
    at org.hibernate.cfg.AnnotationConfiguration.buildSes sionFactory(AnnotationConfiguration.java:859)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityMan agerFactory(Ejb3Configuration.java:669)
    ... 21 more
    Caused by: java.lang.ClassCastException: org.hibernate.dialect.Oracle10gDialect
    at org.hibernate.dialect.DialectFactory.buildDialect( DialectFactory.java:78)
    ... 28 more


    any help would greatly be appreciated.


    regards,


    Steven H.

  • #2
    I don't think that's the correct why to configure an EntityManager in spring. I would check the documentation, but I believe there is a factory bean for it, something like LocalyEntityManagerFactoryBean, where you can configure your impelementation to be hibernate, and the dialect hibernate should use.

    Comment


    • #3
      Check your classpath for duplicate classes/jars. I suspect hibernate to be loaded in different classloaders causing classcast exceptions.

      Note next time make a better title because it it confusing as it suggest that you are trying to use the Oracle EntityManager (Toplink) and configure hibernate...

      Comment

      Working...
      X