没有名为 EntityManager 的持久性提供程序

No Persistence provider for EntityManager named

得到以下异常

javax.persistence.PersistenceException: No Persistence provider for EntityManager named DERBY
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.kstiehl.test.Test.doGet(Test.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

具有此目录结构

src
├── main
│   ├── java
│   │   ├── com
│   │   │   └── kstiehl
│   │   │       ├── entities
│   │   │       │   └── Person.java
│   │   │       └── test
│   │   │           └── Test.java
│   │   └── Starter.java
│   └── resources
│       ├── log4j.properties
│       ├── META-INF
│       │   └── persistence.xml
│       └── persistence.out.xml
└── test
    └── java

我的坚持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="DERBY" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.kstiehl.entities.Person</class>

    <properties>
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.dialect"
            value="com.mysema.query.jpa.support.ExtendedDerbyDialect" />
        <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
        <property name="hibernate.connection.url" value="jdbc:derby:target/derbydb;create=true" />
        <!-- <property name="hibernate.show_sql" value="true"/> -->
        <property name="hibernate.flushMode" value="FLUSH_AUTO" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
</persistence-unit>

最后但同样重要的是我的 gralde 构建脚本

apply plugin: 'war'
apply plugin: 'eclipse'

apply plugin: 'java'

webAppDirName = 'WebContent'



repositories {
    mavenCentral()
}

dependencies {
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'org.apache.derby', name: 'derby', version: '10.12.1.1'
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.1.Final'
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.1.Final'


}


war {

    webInf { from fileTree("WEB-INF")} // adds a file-set to the WEB-INF dir.
    classpath fileTree('libs')
}

当我检查 war 文件时,一切都在正确的位置,但我仍然遇到错误。

这是我的 servlet 的 do get 方法,它仅用于测试目的,以检查 jpa 是否正常工作,但它已经在那里崩溃了。

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    EntityManagerFactory FACTORY = Persistence.createEntityManagerFactory("DERBY");
    EntityManager manager = null;
    manager = FACTORY.createEntityManager();


}

有什么想法吗?

您的依赖项中没有包含 org.hibernate.ejb.HibernatePersistence

要么您从 persistence.xml 中删除显式提供程序(这样它应该会在您的类路径中选择任何提供程序) 或者您将其更改为 org.hibernate.jpa.HibernatePersistenceProvider(实际上在 hibernate-core 中的那个)