java JPA - createEntityManagerFactory 返回 Null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7748223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:15:06  来源:igfitidea点击:

JPA - createEntityManagerFactory returns Null

javahibernatejpaentitymanagerhibernate-entitymanager

提问by Gaston

Noob question here. I'm following this example/tutorialto try and isolate a problem I keep getting on my main project. Problem is, the entityManagerFactory keeps returning null (thus, I get a NullPointerExcept when trying to run the first JUnit test.)

菜鸟问题在这里。我正在按照此示例/教程尝试隔离我在主要项目中不断遇到的问题。问题是,entityManagerFactory 不断返回 null(因此,我在尝试运行第一个 JUnit 测试时得到 NullPointerExcept。)

I'm currently on Eclipse Indigo (JavaEE) - JRE7 - Hibernate 3.6.7 and JBoss 7

我目前使用 Eclipse Indigo (JavaEE) - JRE7 - Hibernate 3.6.7 和 JBoss 7

And here's my persistence.xml (again, a copipasta taken right from the tutorial)

这是我的persistence.xml(同样,从教程中获取的copipasta)

<persistence>
        <persistence-unit name="examplePersistenceUnit" 
                          transaction-type="RESOURCE_LOCAL">
            <properties>
                <property name="hibernate.show_sql" value="false" />
                <property name="hibernate.format_sql" value="false" />

                <property name="hibernate.connection.driver_class" 
                          value="org.hsqldb.jdbcDriver" />
                <property name="hibernate.connection.url" 
                          value="jdbc:hsqldb:mem:mem:aname" />
                <property name="hibernate.connection.username" value="sa" />

                <property name="hibernate.dialect" 
                          value="org.hibernate.dialect.HSQLDialect" />
                <property name="hibernate.hbm2ddl.auto" value="create" />
            </properties>
        </persistence-unit>
    </persistence>

Things I've already tried/found out so far:

到目前为止我已经尝试/发现的事情:

  • Seems this problem tends to occurif you try to create a factory with a persistence unit that is not listed in the persistence.xml

  • Double-checked that the necessary JARs are included in Eclipse's Build Path Libraries, which Google suggested may be a possible cause for a createEntityManagerFactory() call to short-circuit and return null (instead of just throwing an exception or logging a message)

  • Might be due to a possible error whilst configuringHibernate connection?

  • 如果您尝试使用未在persistence.xml 中列出的持久性单元创建工厂,似乎会出现此问题

  • 仔细检查必要的 JAR 是否包含在 Eclipse 的构建路径库中,谷歌建议这可能是 createEntityManagerFactory() 调用短路并返回 null(而不是仅仅抛出异常或记录消息)的可能原因

  • 可能是由于配置Hibernate 连接时可能出现的错误?

I've been hitting this wall for the past couple of weeks, so it goes without saying that any help/general direction tips are MUCH appreciated :D

过去几周我一直在碰壁,所以不用说,非常感谢任何帮助/一般方向提示:D

采纳答案by André

Gaston, I have some suggestions/questions for you:

加斯顿,我有一些建议/问题要问你:

1 - Is this the code you're trying to execute?

1 - 这是您要执行的代码吗?

package entity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class PersonTest {
    private EntityManagerFactory emf;

    private EntityManager em;

    @Before
    public void initEmfAndEm() {
        BasicConfigurator.configure();
        Logger.getLogger("org").setLevel(Level.ERROR);

        emf = Persistence.createEntityManagerFactory("examplePersistenceUnit");
        em = emf.createEntityManager();
    }

    @After
    public void cleanup() {
        em.close();
    }

    @Test
    public void emptyTest() {
    }
}

If it is, try to comment this line: "Logger.getLogger("org").setLevel(Level.ERROR);". Or change it to "Logger.getLogger("org").setLevel(Level.ALL);". Then you should see the errors on the output console.

如果是,请尝试注释此行:“Logger.getLogger("org").setLevel(Level.ERROR);”。或者将其更改为“Logger.getLogger("org").setLevel(Level.ALL);”。然后您应该会在输出控制台上看到错误。

2 - In your persistence.xml I see you're using hsqldb database. Did you installed/configured it properly?

2 - 在您的 persistence.xml 中,我看到您正在使用 hsqldb 数据库。您是否正确安装/配置了它?

If you dont't know this database, I suggest you use MySQL, PostgreSQL, or some database you are familiar with.

如果你不了解这个数据库,我建议你使用 MySQL、PostgreSQL 或一些你熟悉的数据库。

3 - Check you persistence.xml. Mine is a little bite different:

3 - 检查你的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="App1PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.entities.User</class>
        <class>com.entities.Group</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://myIP:3306/mydatabase"/>
            <property name="javax.persistence.jdbc.password" value="secret"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="myUser"/>
        </properties>
    </persistence-unit>
</persistence>

Notice the header has some XML declarations that might be important, since Hibernate is telling you that your file is incorrect.

请注意,标题有一些可能很重要的 XML 声明,因为 Hibernate 告诉您您的文件不正确。