Java 持久化实体管理器工厂返回 null

发布于 2024-12-08 02:39:14 字数 1875 浏览 0 评论 0原文

我正在使用带有 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();

我得到一个空指针异常。

任何帮助将不胜感激。

I am using MySql database with JPA. my persistence.xml looks like

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

in my java code i do the following

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

emf always come back null. the database is connected and pings successfully. I have even tried to change the persistence-unit name to something that doesn't match the entry in the persistance.xml and I am still getting null. It is not throwing any exceptions. ofcourse when it gets to the second line in the code

EntityManager em = emf.createEntityManager();

I get a null pointer exception.

any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-12-15 02:39:14

打开最精细的日志记录并确保加载持久性单元时不会发生错误。

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

Turn logging on finest and ensure no errors are occurring loading the persistence unit.

What environment are you using, do you have all the required jars on your classpath?

海之角 2024-12-15 02:39:14

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

Are you sure that the persistence.xml files resides in the META-INF folder?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文