简体   繁体   中英

No Persistence provider for EntityManager named

got the following exception

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)

with this directory structure

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

my persistence xml looks like this

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

and last but not least my gralde build script

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')
}

when i check the war file everything is at the right place but i still got the error.

this is the do get method of my servlet its just for testing purpose to check if the jpa thingy works but it already crashes there.

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


}

any ideas?

You do not include org.hibernate.ejb.HibernatePersistence in your dependencies.

Either you remove the explicit provider from your persistence.xml (that way it should pick up any provider on your classpath) or you change it to org.hibernate.jpa.HibernatePersistenceProvider (the one actually in hibernate-core)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM