简体   繁体   English

如何使用OracleDataTypeFactory为Oracle配置Spring Data

[英]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. 我是该社区的新手,不确定是否在正确的位置发布...还将其发布在Oracle的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. 我是Oracle的老手,但是对Spring Data还是陌生的,在postGIS上做了一些工作,但是现在尝试将其应用于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. 我的问题:我有一个新的maven项目,其中包含Entity和Repository类,以及一个使用DbUnit预加载测试数据的JUnit测试程序。

The current config uses a beans.xml file to set up: 当前配置使用beans.xml文件进行设置:

<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. repos.count()的测试正常。

Test to call repos.findByName(String) fails, (stack below). 调用repos.findByName(String)的测试失败(下面的堆栈)。

Now, the current setup doesn't handle SDO_GEOMETRY, or TIMESTAMP(0), but will manage TIMESTAMP(6). 现在,当前设置不处理SDO_GEOMETRY或TIMESTAMP(0),但将管理TIMESTAMP(6)。

The warnings below show that I'm using "DefaultDataTypeFactory" and should probably be using OracleDataTypeFactory. 以下警告显示我正在使用“ DefaultDataTypeFactory”,并且可能应该使用OracleDataTypeFactory。

I've also found the OracleDataSource class, and tried that, but still get the same data type issues. 我还找到了OracleDataSource类,并进行了尝试,但仍然遇到相同的数据类型问题。

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? 谁能给我一个例子来配置dataSource或vendorAdapter以使用OracleDataTypeFactory并检索这些数据类型?

  2. What data type should I be using in Java for the geometry column? 我应在Java中为“几何”列使用哪种数据类型?

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. 首先,UnsupportedOperationException是由于在Entity中使用了错误的Java类。 For an Oracle SDO_GEOMETRY column, use: 对于Oracle SDO_GEOMETRY列,使用:

 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. 其次,可以通过将dbUnit配置为使用正确的类型工厂来解决来自dbUnit的警告。 Example here uses beans.xml to create beans with the correct settings, which are then used by dbunit: 此处的示例使用beans.xml创建具有正确设置的Bean,然后由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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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