0

I'm using eclipse juno IDE I have Java application which have src folder. within the folder I have:

1) applicationContext.xml

2) persistence.xml

I also have DBInterface and i implemented it with JPA. Now in the applicationContext.xml file I have a bean for the JPA implemention.

When I tried to inject the bean i got an excpetion that said something like "No Persistence Provider was found".

So I tried to import the persistence file within the applicationContext file but i'm getting another exception.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
   xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
   xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
   xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
   xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
   xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
   xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
   xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
   xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
   xmlns:p="http://www.springframework.org/schema/p"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd
      http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-2.5.xsd">


    <bean id="JPA"  class="pack.jpa.JPAQueries"/>
    <import resource="persistence.xml"/>
</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 

http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> 
         <persistence-unit transaction-type="RESOURCE_LOCAL" name="MyJPA"> 
             <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
             <class>pack.bl.Travels</class>
             <class>pack.bl.Example</class> 
             <properties> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/> 
                 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/taxis"/> 
                 <property name="javax.persistence.jdbc.password" value=""/> 
                 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
                 <property name="javax.persistence.jdbc.user" value="root"/>
             </properties> 
         </persistence-unit> 

mainClass

public class Program {

/**
 * @param args
 */
public static void main(String[] args) {

  ApplicationContext context = new  
  ClassPathXmlApplicationContext("applicationContext.xml");
  DBInterface dao = (DBInterface)context.getBean("JPA",JPAQueries.class);

  dao.retrieveRecords();
}

Exception

Exception in thread "main"  
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration 
problem: Failed to import bean definitions from relative location [persistence.xml]

Offending resource: class path resource [applicationContext.xml]; nested exception is 
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration 
problem: Unable to locate Spring NamespaceHandler for XML schema namespace 
[http://java.sun.com/xml/ns/persistence]

Offending resource: class path resource [persistence.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:271)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:196)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:181)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at pack.program.Program.main(Program.java:16)

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://java.sun.com/xml/ns/persistence]

Offending resource: class path resource [persistence.xml]

at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:317)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1421)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:190)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:255)
... 20 more
eliorsh
  • 195
  • 1
  • 5
  • 20

3 Answers3

7

Your attempt to use persistence.xml as a Spring config makes absolutely no sense, because persistence.xml is not a Spring config.

If you want to use JPA with Spring, you need to put persistence.xml into META-INF folder inside your source folder, and declare LocalContainerEntityManagerFactory in applicationContext.xml:

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name = "persistenceUnitName" value = "MyJPA" />
</bean>

Then you can inject EntityManager into your Spring bean using @PersistenceContext:

@PersistenceContext
private EntityManager em;

See also:

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • thanks axtavt, but if it's a regular java project which doesn't have META-INF folder, can i put the persistence.xml within the src folder? and another question, how the bean that your write in example code will refer to the persistence file? – eliorsh Sep 03 '12 at 18:18
  • @eliorsh: You can simply create that folder inside your source folder and put `persistence.xml` there. `LocalContainerEntityManagerFactoryBean` creates JPA `EntityManagerFactory` for persistence unit with the given name, and you define persistence units in `persistence.xml`. – axtavt Sep 03 '12 at 18:30
0

It should be imported as follows:

<import resource="classpath:META-INF/persistence.xml"/>

assuming the persistence.xml is present in META-INF directory which is a top-level directory in one of your jars on the classpath.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Vikdor thanks for your answer but the project isn't a JPA project is a java project because i need to use it as a server, so i don't have the META-INF folder and because of that i located the persistence file within the src folder – eliorsh Sep 03 '12 at 17:48
  • ok i converted the project into a JPA project and placed the persistence file within the META-INF folder.. and imported as you said, but i'm still getting the same exception – eliorsh Sep 03 '12 at 17:56
  • If it was not a JPA project, then you just need to make sure that the persistence.xml gets into your jar at some location and you would refer to that location with `classpath:` prefix. Now that you moved it into persistence.xml, can you list the contents of your jar and find the location of persistence.xml and update the path in the resource appropriately? – Vikdor Sep 03 '12 at 17:58
0

Ok, I Solved this..

What i did is to put the persistence.xml file within the META-INF folder as vikdor and axtavt suggested. but in the application context i didn't import any file.. just wrote this

<bean id="JPA" class="pack.jpa.JPAQueries"/>

and its work!

eliorsh
  • 195
  • 1
  • 5
  • 20