Announcement Announcement Module
Collapse
No announcement yet.
spring data with oracle Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring data with oracle

    Hello
    I would like to use spring data with oracle, I went by the example posted by Spring but it is not working for me

    https://github.com/SpringSource/spri...astructure.xml


    based on the tutorial, I created @entity (SupplyItem) , created an interface @Repository SupplyItemRepo that extends the JpaRepository<SupplyItem, Long>

    I created persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.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_2_0.xsd">
    <persistence-unit name="jpa.sample" />
    <persistence-unit name="dataSource">
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.ORACLEDialect" />
    <property name="hibernate.connection.url"
    value="jdbc:oracle:thin:@a.b-c.com:1521:abc" />
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
    <property name="hibernate.connection.username" value="root" />
    <property name="hibernate.connection.password" value="test" />
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    </properties>
    </persistence-unit>
    </persistence>


    and also created the app-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schem...spring-jpa.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd" >

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="generateDdl" value="true" />
    <property name="database" value="ORACLE" />
    </bean>
    </property>
    <property name="persistenceUnitName" value="jpa.sample" />
    </bean>

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


    <jpa:repositories base-package="org.springframework.data.jpa.example.repo sitory"/>

    </beans>


    I keep getting this error

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExc eptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in class path resource [config/app-context.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'dataSource' is defined.

    your help is appreciated.

    Regards;

  • #2
    "No bean named 'dataSource' is defined"

    That is your error, you have no Spring bean defined called dataSource. In your configs you posted there is no such bean.

    The only place I saw you use the name "dataSource" is in the persistence.xml as a persistence unit name. Which doesn't make sense.

    You need to go into your Spring config and create a bean called dataSource that creates a dataSource.

    Mark

    Comment


    • #3
      The example from spring does not define a datasoursce bean, is it because they use in memory database

      <jdbc:embedded-database id="dataSource" type="HSQL" />

      I am using orable and did add a datasource section and put in the ojdc14 in my build path

      <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="url"
      value="jdbc:oracle:thin:@devdb.com:aaa:aaa" />
      <property name="username" value="??" />
      <property name="password" value="??" />

      </bean>

      but I get this error


      java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.TestContext.getAp plicationContext(TestContext.java:157)
      at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.injectDependencies (DependencyInjectionTestExecutionListener.java:109 )
      at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.prepareTestInstanc e(DependencyInjectionTestExecutionListener.java:75 )
      at org.springframework.test.context.TestContextManage r.prepareTestInstance(TestContextManager.java:321)
      at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTest(SpringJUnit4ClassRunner.j ava:211)
      at org.springframework.test.context.junit4.SpringJUni t4ClassRunner$1.runReflectiveCall(SpringJUnit4Clas sRunner.java:288)
      at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
      at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.methodBlock(SpringJUnit4ClassRunner. java:290)
      at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:231)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner. java:193)
      at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:52)
      at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:191)
      at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:42)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:184)
      at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:71)
      at org.junit.runners.ParentRunner.run(ParentRunner.ja va:236)
      at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:174 )
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
      Caused by: org.springframework.beans.factory.parsing.BeanDefi nitionParsingException: Configuration problem: Failed to import bean definitions from relative location [infrastructure.xml]
      Offending resource: class path resource [simple-repository-context.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefin itionStoreException: Line 32 in XML document from class path resource [infrastructure.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-id.2: There are multiple occurrences of ID value 'transactionManager'.
      at org.springframework.beans.factory.parsing.FailFast ProblemReporter.error(FailFastProblemReporter.java :68)
      at org.springframework.beans.factory.parsing.ReaderCo ntext.error(ReaderContext.java:85)
      at org.springframework.beans.factory.parsing.ReaderCo ntext.error(ReaderContext.java:76)
      at org.springframework.beans.factory.xml.DefaultBeanD efinitionDocumentReader.importBeanDefinitionResour ce(DefaultBeanDefinitionDocumentReader.java:271)
      at org.springframework.beans.factory.xml.DefaultBeanD efinitionDocumentReader.parseDefaultElement(Defaul tBeanDefinitionDocumentReader.java:196)
      at org.springframework.beans.factory.xml.DefaultBeanD efinitionDocumentReader.parseBeanDefinitions(Defau ltBeanDefinitionDocumentReader.java:181)
      at org.springframework.beans.factory.xml.DefaultBeanD efinitionDocumentReader.doRegisterBeanDefinitions( DefaultBeanDefinitionDocumentReader.java:140)
      at org.springframework.beans.factory.xml.DefaultBeanD efinitionDocumentReader.registerBeanDefinitions(De faultBeanDefinitionDocumentReader.java:111)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.registerBeanDefinitions(XmlBeanDefinit ionReader.java:493)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:390)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:334)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:302)
      at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:174)
      at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:209)
      at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:180)
      at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:243)
      at org.springframework.test.context.support.AbstractG enericContextLoader.loadBeanDefinitions(AbstractGe nericContextLoader.java:205)
      at org.springframework.test.context.support.AbstractG enericContextLoader.loadContext(AbstractGenericCon textLoader.java:100)
      at org.springframework.test.context.support.AbstractG enericContextLoader.loadContext(AbstractGenericCon textLoader.java:1)
      at org.springframework.test.context.support.Delegatin gSmartContextLoader.loadContext(DelegatingSmartCon textLoader.java:228)
      at org.springframework.test.context.TestContext.loadA pplicationContext(TestContext.java:124)
      at org.springframework.test.context.TestContext.getAp plicationContext(TestContext.java:148)
      ... 24 more
      Caused by: org.springframework.beans.factory.xml.XmlBeanDefin itionStoreException: Line 32 in XML document from class path resource [infrastructure.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-id.2: There are multiple occurrences of ID value 'transactionManager'.
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:396)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:334)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:302)
      at org.springframework.beans.factory.xml.DefaultBeanD efinitionDocumentReader.importBeanDefinitionResour ce(DefaultBeanDefinitionDocumentReader.java:255)
      ... 42 more
      Caused by: org.xml.sax.SAXParseException: cvc-id.2: There are multiple occurrences of ID value 'transactionManager'.
      at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.createSAXParseException(ErrorHandlerWrap per.java:195)
      at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.error(ErrorHandlerWrapper.java:131)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(XMLErrorReporter.java:384)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(XMLErrorReporter.java:318)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator$XSIErrorReporter.reportError(XMLSchema Validator.java:410)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.reportSchemaError(XMLSchemaValidator.j ava:3165)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.processOneAttribute(XMLSchemaValidator .java:2748)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.processAttributes(XMLSchemaValidator.j ava:2685)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.handleStartElement(XMLSchemaValidator. java:2037)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.startElement(XMLSchemaValidator.java:6 85)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.scanStartElement(XMLNSDocumentScann erImpl.java:400)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDriver.next(XM LDocumentFragmentScannerImpl.java:2747)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl.next(XMLDocumentScannerImpl.java:648)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.next(XMLNSDocumentScannerImpl.java: 140)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(XMLDocumentFragm entScannerImpl.java:510)
      at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:807)
      at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:737)
      at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(XMLParser.java:107)
      at com.sun.org.apache.xerces.internal.parsers.DOMPars er.parse(DOMParser.java:225)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderImpl.parse(DocumentBuilderImpl.java:283)
      at org.springframework.beans.factory.xml.DefaultDocum entLoader.loadDocument(DefaultDocumentLoader.java: 75)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:388)
      ... 45 more

      Comment


      • #4
        "Caused by: org.xml.sax.SAXParseException: cvc-id.2: There are multiple occurrences of ID value 'transactionManager'."

        There you go. You have two beans with id called transactionManager in the same xml file.

        Whenever you get exceptions, scroll down to the root causedBy line and you will find a really good explanation of why an exception got thrown. If you try to read from the top you can easily miss it cause sometimes it is a totally different message.

        Mark

        Comment

        Working...
        X