JPA系列--JPA部署阶段常见错误及解决方法(持续更新)

错误一:javax.persistence.PersistenceException: Unable to locate persistence units问题

1.问题详述

1.1 persistence.xml配置文件


<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="engine_diagnosis_system"
        transaction-type="RESOURCE_LOCAL">
        <class>cn.edu.sdut.softlab.model.UserInfoclass>
        <class>cn.edu.sdut.softlab.model.EngineInfoclass>
        <properties>
            
            <property name="javax.persistence.jdbc.url"
                value="jdbc:postgresql://localhost:5432/engine_diagnosis_system" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="" />

            
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

        properties>
    persistence-unit>
persistence> 

1.2 pom.xml配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>cn.edu.sdut.softlab.HelloWorldJPAgroupId>
    <artifactId>jpaartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>HelloWorldJPA Maven Webappname>
    <url>http://maven.apache.orgurl>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>com.atomikosgroupId>
            <artifactId>transactionsartifactId>
            <version>3.9.3version>
        dependency>
        
        <dependency>
            <groupId>javax.enterprisegroupId>
            <artifactId>cdi-apiartifactId>
            <version>1.2version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-entitymanagerartifactId>
            <version>5.0.0.Finalversion>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
            <version>1.7.13version>
        dependency>
        
        <dependency>
            <groupId>com.sun.facesgroupId>
            <artifactId>jsf-apiartifactId>
            <version>2.2.12version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.primefacesgroupId>
            <artifactId>primefacesartifactId>
            <version>5.0version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
            <version>1.7.13version>

    dependencies>
    <build>
        <finalName>jpafinalName>
        <plugins>

            
            <plugin>
                <artifactId>maven-war-pluginartifactId>
                <version>2.2version>
                <configuration>
                    <failOnMissingWebXml>falsefailOnMissingWebXml>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.wildfly.pluginsgroupId>
                <artifactId>wildfly-maven-pluginartifactId>
                <version>1.1.0.Alpha5version>
            plugin>
        plugins>
    build>
project>

1.3 报错信息

13:29:15,663 ERROR [stderr] (default task-22) javax.persistence.PersistenceException: Unable to locate persistence units
13:29:15,664 ERROR [stderr] (default task-22)   at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:84)
13:29:15,664 ERROR [stderr] (default task-22)   at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
13:29:15,664 ERROR [stderr] (default task-22)   at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
13:29:15,664 ERROR [stderr] (default task-22)   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
13:29:15,664 ERROR [stderr] (default task-22)   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
13:29:15,664 ERROR [stderr] (default task-22)   at cn.edu.sdut.softlab.controller.UserController.register(UserController.java:36)
13:29:15,664 ERROR [stderr] (default task-22)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:29:15,664 ERROR [stderr] (default task-22)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
13:29:15,664 ERROR [stderr] (default task-22)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
13:29:15,664 ERROR [stderr] (default task-22)   at java.lang.reflect.Method.invoke(Method.java:498)
13:29:15,664 ERROR [stderr] (default task-22)   at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)
13:29:15,664 ERROR [stderr] (default task-22)   at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
13:29:15,665 ERROR [stderr] (default task-22)   at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
13:29:15,665 ERROR [stderr] (default task-22)   at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
13:29:15,665 ERROR [stderr] (default task-22)   at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
13:29:15,665 ERROR [stderr] (default task-22)   at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
13:29:15,665 ERROR [stderr] (default task-22)   at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
13:29:15,665 ERROR [stderr] (default task-22)   at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
13:29:15,665 ERROR [stderr] (default task-22)   at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
13:29:15,665 ERROR [stderr] (default task-22)   at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
13:29:15,665 ERROR [stderr] (default task-22)   at javax.faces.component.UICommand.broadcast(UICommand.java:315)
13:29:15,665 ERROR [stderr] (default task-22)   at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
13:29:15,665 ERROR [stderr] (default task-22)   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
13:29:15,665 ERROR [stderr] (default task-22)   at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
13:29:15,665 ERROR [stderr] (default task-22)   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
13:29:15,666 ERROR [stderr] (default task-22)   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
13:29:15,666 ERROR [stderr] (default task-22)   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
13:29:15,666 ERROR [stderr] (default task-22)   at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
13:29:15,666 ERROR [stderr] (default task-22)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:29:15,667 ERROR [stderr] (default task-22)   at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
13:29:15,667 ERROR [stderr] (default task-22)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:29:15,667 ERROR [stderr] (default task-22)   at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:29:15,667 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
13:29:15,667 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
13:29:15,668 ERROR [stderr] (default task-22)   at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
13:29:15,669 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
13:29:15,669 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
13:29:15,669 ERROR [stderr] (default task-22)   at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
13:29:15,669 ERROR [stderr] (default task-22)   at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
13:29:15,669 ERROR [stderr] (default task-22)   at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
13:29:15,669 ERROR [stderr] (default task-22)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
13:29:15,669 ERROR [stderr] (default task-22)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
13:29:15,669 ERROR [stderr] (default task-22)   at java.lang.Thread.run(Thread.java:745)
13:29:15,669 ERROR [stderr] (default task-22) Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
13:29:15,669 ERROR [stderr] (default task-22) Error parsing XML [line : -1, column : -1] : cvc-complex-type.2.4.a: Invalid content was found starting with element 'provider'. One of '{"http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":shared-cache-mode, "http://java.sun.com/xml/ns/persistence":validation-mode, "http://java.sun.com/xml/ns/persistence":properties}' is expected.
13:29:15,669 ERROR [stderr] (default task-22) 
13:29:15,669 ERROR [stderr] (default task-22)   at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:357)
13:29:15,669 ERROR [stderr] (default task-22)   at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:290)
13:29:15,669 ERROR [stderr] (default task-22)   at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:94)
13:29:15,670 ERROR [stderr] (default task-22)   at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:84)
13:29:15,670 ERROR [stderr] (default task-22)   at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:66)
13:29:15,670 ERROR [stderr] (default task-22)   at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
13:29:15,670 ERROR [stderr] (default task-22)   ... 64 more

2.问题分析

在报错信息中有这样一段:

 at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)

我由此猜想可能是pom.xml文件中没有配置postgreSQL的驱动所致。

3.问题解决

在pom.xml文件中加入postgreSQL的驱动的依赖即可。


        <dependency>
            <groupId>postgresqlgroupId>
            <artifactId>postgresqlartifactId>
            <version>9.1-901-1.jdbc4version>
        dependency>

你可能感兴趣的:(JPA,persistenc)