【问题标题】:Java persistence entity manager factory returning nullJava 持久性实体管理器工厂返回 null
【发布时间】:2011-12-01 22:45:00
【问题描述】:

我正在使用带有 JPA 的 MySql 数据库。我的 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="citheph">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>persistence.citheph.pTerm</class>
        <class>persistence.citheph.pPublicationterm</class>
        <class>persistence.citheph.pPublicationauthor</class>
        <class>persistence.citheph.pPublication</class>
        <class>persistence.citheph.pCoauthorship</class>
        <class>persistence.citheph.pCitation</class>
        <class>persistence.citheph.pAuthor</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cithepth" />
            <property name="javax.persistence.jdbc.password" value="amer1" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.user" value="root" />
        </properties>

    </persistence-unit>
</persistence>

在我的 java 代码中,我执行以下操作

EntityManagerFactory emf = Persistence.createEntityManagerFactory("citheph");
EntityManager em = emf.createEntityManager();

emf 总是返回 null。数据库已连接并且 ping 成功。我什至尝试将持久性单元名称更改为与persistance.xml 中的条目不匹配的名称,但仍然为空。它不会抛出任何异常。当然,当它到达代码中的第二行时

EntityManager em = emf.createEntityManager();

我得到一个空指针异常。

任何帮助将不胜感激。

【问题讨论】:

    标签: java mysql eclipse jpa persistence


    【解决方案1】:

    最好开启日志记录,并确保加载持久性单元时不会发生错误。

    您使用的是什么环境,您的类路径中是否有所有必需的 jar?

    【讨论】:

    • 谢谢。我使用的持久性类库与 eclips 类库不同。感谢您的宝贵时间
    • 如何开启最好的登录?
    【解决方案2】:

    您确定persistence.xml 文件位于META-INF 文件夹中吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 2012-03-14
      • 1970-01-01
      • 2014-05-13
      相关资源
      最近更新 更多