JPA-createEntityManagerFactory返回Null [英] JPA - createEntityManagerFactory returns Null

查看:87
本文介绍了JPA-createEntityManagerFactory返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Noob的问题在这里.我正在关注此示例/教程尝试找出一个问题,使我不断进入自己的主要项目.问题是,entityManagerFactory始终返回null(因此,当尝试运行第一个JUnit测试时,我得到了NullPointerExcept.)

Noob question here. I'm following this example/tutorial to 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.)

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

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

这是我的persistence.xml(同样,也是从本教程中直接获得的copipasta)

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

<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 occur if you try to create a factory with a persistence unit that is not listed in the persistence.xml

仔细检查了Eclipse的必需的JAR是否包含在其中 Google建议的构建路径库"可能是原因 对于createEntityManagerFactory()调用以短路并返回 null(而不只是引发异常或记录消息)

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 configuring Hibernate connection?

过去两周来,我一直在碰壁,所以毋庸置疑,任何帮助/一般指导都非常感谢:D

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

推荐答案

加斯顿,我为您提供一些建议/问题:

Gaston, I have some suggestions/questions for you:

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() {
    }
}

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

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.

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

如果您不知道该数据库,建议您使用MySQL,PostgreSQL或您熟悉的某些数据库.

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

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>

请注意,标头中包含一些可能很重要的XML声明,因为Hibernate告诉您文件不正确.

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

这篇关于JPA-createEntityManagerFactory返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆