为什么Hibernate找不到持久化名称?

时间:2018-04-03 23:07:08

标签: java hibernate configuration

我在服务器启动期间遇到以下异常......

03-Apr-2018 15:51:30.407 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.nobody.myapp.servlet.HibernateListener
 javax.persistence.PersistenceException: No Persistence provider for EntityManager named theDB
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at org.nobody.myapp.servlet.HibernateListener.contextInitialized(HibernateListener.java:7)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1853)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

代码看起来像这样......

public class HibernateListener implements ServletContextListener, Serializable {

    private static EntityManagerFactory nepEntityManager;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        nepEntityManager = Persistence.createEntityManagerFactory("theDB");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (nepEntityManager != null) {
            nepEntityManager.close();   
        }
    }

...和这样的persistence.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="theDB" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/zeedoo</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>ALL</shared-cache-mode>
        <properties>
            <property name="hibernate.ejb.entitymanager_factory_name" value="theDB"/>
            <property name="hibernate.connection.release_mode" value="auto" />
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
            <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
            <property name="hibernate.current_session_context_class" value="jta"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.use_sql_comments" value="false" />
        </properties>
    </persistence-unit>
</persistence>

我觉得“ theDB ”匹配。为什么没找到?

1 个答案:

答案 0 :(得分:0)

因为EntityManagerFactory无法获取jndi名称jdbc / zeedoo,请检查您的数据源是否存在此名称