简体   繁体   中英

How configure Spring Data for oracle with OracleDataTypeFactory

I'm new to this community, not sure if I'm posting in the right place... also posted this on Oracle's OTN.

I'm an Oracle old-hand, but new to Spring Data, having done a bit on postGIS, but now trying to apply it to Oracle.

My problem: I have a new maven project, with Entity and Repository classes, and a JUnit test program that uses DbUnit to preload test data.

The current config uses a beans.xml file to set up:

<bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">...
  <property name="jpaProperties">....

<bean id="vendorAdapter"
  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
  <property name="generateDdl" value="false" />
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="oracle.jdbc.OracleDriver" />
  <property name="jdbcUrl"
   value="jdbc:oracle:thin:@localhost:2632:BLADE" />...

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

Test of repos.count() works ok.

Test to call repos.findByName(String) fails, (stack below).

Now, the current setup doesn't handle SDO_GEOMETRY, or TIMESTAMP(0), but will manage TIMESTAMP(6).

The warnings below show that I'm using "DefaultDataTypeFactory" and should probably be using OracleDataTypeFactory.

I've also found the OracleDataSource class, and tried that, but still get the same data type issues.

Ok, the big questions:

  1. Can anyone point me at an example to configure the dataSource, or vendorAdapter, to use the OracleDataTypeFactory, and retrieve these data types?

  2. What data type should I be using in Java for the geometry column?

Thanks,

Phil

Test output warnings:

16:24:05.846 [main] INFO  org.springframework.test.context.transaction.TransactionalTestExecutionListener - Began transaction (2) for test context [DefaultTestContext@990034 testClass = NotamRepositoryTest, testInstance = com.thalesgroup.uk.airscape.common.data.domain.dao.notam.NotamRepositoryTest@3ff1d6, testMethod = testNotamFetchByDatasetName@NotamRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1346193 testClass = NotamRepositoryTest, locations = '{classpath:testBeans.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@1f1fe9d]; rollback [true]

16:24:08.674 [main] WARN  org.dbunit.dataset.AbstractTableMetaData - Potential problem found: The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems with the current database 'Oracle' (e.g. some datatypes may not be supported properly). In rare cases you might see this message because the list of supported database products is incomplete (list=[derby]). If so please request a java-class update via the forums.If you are using your own IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override getValidDbProducts() to specify the supported database products.

16:24:08.674 [main] WARN  org.dbunit.util.SQLHelper - DATA_SET.SEC_TAG_MODIFIED data type (1111, 'TIMESTAMP(0)') not recognized and will be ignored. See FAQ for more information.

16:24:08.674 [main] WARN  org.dbunit.util.SQLHelper - DATA_SET.CREATED data type (1111, 'TIMESTAMP(0)') not recognized and will be ignored. See FAQ for more information.

16:24:08.674 [main] WARN  org.dbunit.util.SQLHelper - DATA_SET.MODIFIED data type (1111, 'TIMESTAMP(0)') not recognized and will be ignored. See FAQ for more information.

16:24:08.690 [main] WARN  org.dbunit.util.SQLHelper - DATA_SET.VALID_FROM data type (1111, 'TIMESTAMP(0)') not recognized and will be ignored. See FAQ for more information.

16:24:08.690 [main] WARN  org.dbunit.util.SQLHelper - DATA_SET.VALID_TO data type (1111, 'TIMESTAMP(0)') not recognized and will be ignored. See FAQ for more information.

16:24:08.690 [main] WARN  org.dbunit.dataset.AbstractTableMetaData - Potential problem found: The configured data type factory 'class org.dbunit.dataset.datatype.DefaultDataTypeFactory' might cause problems with the current database 'Oracle' (e.g. some datatypes may not be supported properly). In rare cases you might see this message because the list of supported database products is incomplete (list=[derby]). If so please request a java-class update via the forums.If you are using your own IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override getValidDbProducts() to specify the supported database products.

16:24:08.690 [main] WARN  org.dbunit.util.SQLHelper - NOTAM.Q_POINT data type (1111, 'SDO_GEOMETRY') not recognized and will be ignored. See FAQ for more information.

16:24:08.690 [main] WARN  org.dbunit.util.SQLHelper - NOTAM.Q_RANGE_RING data type (1111, 'SDO_GEOMETRY') not recognized and will be ignored. See FAQ for more information.


    **java.lang.UnsupportedOperationException**
at org.hibernate.spatial.GeometrySqlTypeDescriptor.getExtractor(GeometrySqlTypeDescriptor.java:57)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627)
at org.hibernate.loader.Loader.getRow(Loader.java:1514)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)
at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
at org.hibernate.loader.Loader.doQuery(Loader.java:920)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:81)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:59)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:97)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:88)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:384)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy67.findByDataSetNameAndDeletedIsNull(Unknown Source)
at com.thalesgroup.uk.airscape.common.data.domain.dao.notam.NotamRepositoryTest.testNotamFetchByDatasetName(NotamRepositoryTest.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Ok, we've finally figured it out. There are two problems here.

Firstly, the UnsupportedOperationException was due to using the wrong Java class in the Entity. For an Oracle SDO_GEOMETRY column, use:

 import oracle.spatial.geometry.JGeometry;

 @Column(name = "Q_POINT")
 private JGeometry qPoint;

Secondly, the warnings from dbUnit can be resolved by configuring dbUnit to use the correct type factory. Example here uses beans.xml to create beans with the correct settings, which are then used by dbunit:

    <bean id="vendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="ORACLE" />
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="generateDdl" value="false" />
    </bean>

  <!-- Beans to support DBunit for unit testing with Oracle. -->
  <bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean">
    <property name="datatypeFactory">
      <bean class="org.dbunit.ext.oracle.OracleDataTypeFactory"/>
    </property>
  </bean>
  <bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
    <property name="databaseConfig" ref="dbUnitDatabaseConfig"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="schema" value="MAIN"/>
  </bean>

  <!-- Oracle connection pool -->
  <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"
    destroy-method="close">
    <property name="connectionCachingEnabled" value="true" />
    <property name="URL">
       <value>jdbc:oracle:thin:@<dbhost>:<dbport>:<dbSID></value>
    </property>
    <property name="user">
      <value>myuser</value>
    </property>
    <property name="password">
      <value>mypwd</value>
    </property>
    <property name="connectionCacheProperties">
      <value>
        MinLimit:1
        MaxLimit:20
        InitialLimit:1
        ConnectionWaitTimeout:120
        InactivityTimeout:180
        ValidateConnection:true
      </value>
    </property>
  </bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM