javax.persistence.PersistenceException: Invalid persistence.xml

╄→尐↘猪︶ㄣ 提交于 2019-12-30 11:04:42

问题


I'm working on project with Servlet,JPA,EJB and JBoss in eclipse. As you can see in the title of my topic I have an error on my persistence.xml file but I don't know which:

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="TrainingAppEJB-PU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider> 
        <jta-data-source>java:jboss/datasources/MysqlDS</jta-data-source>
        <class>fr.entity.Client</class>
        <class>fr.entity.Course</class>
        <class>fr.entity.CourseSession</class>
        <class>fr.entity.Location</class>

        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/gestion_formation"/>
            <property name="hibernate.default_schema" value="gestion_formation"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="********"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

But when I try to run my servlet I have this error :

  16:59:44,498 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/TrainingAppWeb].[Home]] (http--127.0.0.1-8080-2) "Servlet.service()" pour la servlet Home a généré une exception: javax.ejb.EJBException: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1: Cannot find the declaration of element 'persistence'.

    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:206)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.required(CMTTxInterceptor.java:377)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invoke(CMTTxInterceptor.java:233)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:47)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:146)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:76)
    at utbm.lo54.isession.ManageCourseBeanLocal$$$view11.findAllCourse(Unknown Source) [TrainingAppEJB.jar:]
    at utbm.lo54.servlet.HomeServlet.process(HomeServlet.java:47) [classes:]
    at utbm.lo54.servlet.HomeServlet.doGet(HomeServlet.java:31) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.0.Final.jar:7.0.0.Final]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.0.0.Final.jar:7.0.0.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at java.lang.Thread.run(Thread.java:724) [:1.7.0_40]
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1: Cannot find the declaration of element 'persistence'.

    at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:145) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:169) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:313) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]
    at utbm.lo54.session.ManageCourseBean.getEntityManager(ManageCourseBean.java:36) [TrainingAppEJB.jar:]
    at utbm.lo54.session.ManageCourseBean.findAllCourse(ManageCourseBean.java:51) [TrainingAppEJB.jar:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.7.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [:1.7.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.7.0_40]
    at java.lang.reflect.Method.invoke(Method.java:606) [:1.7.0_40]
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:45) [jboss-as-jpa-7.0.0.Final.jar:7.0.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:44)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewDescription$ComponentDispatcherInterceptor.processInvocation(ViewDescription.java:202)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.as.ejb3.component.session.SessionInvocationContextInterceptor.processInvocation(SessionInvocationContextInterceptor.java:67)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:370)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:271)
    ... 28 more

How can I fix this issue


回答1:


Unless you didn't copy-paste the complete file, the closing tag is missing. Append

</persistence>

in your persistence.xml

EDIT

It seems that the JNDI name of your datasource is wrong. Usually it starts with : java:/




回答2:


switch to

<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">



回答3:


This error means the version of Hibernate that you're using doesn't support version="2.1" in persistence.xml. Change the version to 2.0 (and update the xsi:schemaLocation for clarity).




回答4:


Don't set a provider value.

remove

<provider>org.hibernate.ejb.HibernatePersistence</provider> 

and try.



来源:https://stackoverflow.com/questions/19743886/javax-persistence-persistenceexception-invalid-persistence-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!