JPA - createEntityManagerFactory returns Null

Question Noob. I am following this example / tutorial to try to isolate the problem that I constantly use in my main project. The problem is that entityManagerFactory keeps returning null (this way I get NullPointerExcept when I try to run the first JUnit test.)

I am now on Eclipse Indigo (JavaEE) - JRE7 - Hibernate 3.6.7 and JBoss 7

And here is my persistence.xml (again, copy paste taken directly from the textbook)

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

  • It seems that the problem tends to occur if you try to create a factory with a save unit that is not specified in the persistence.xml file

  • Double-checking that necessary JAR files are included in the Eclipse Library of implementation paths, which, according to Google, may be a possible reason to call createEntityManagerFactory () to short-circuit and return null (instead of just throwing an exception or registering a message)

  • Perhaps due to a possible error when setting up the Hibernate connection?

I hit this wall the last couple of weeks, so it goes without saying that any tips with help / general direction are very highly rated: D

+4
source share
1 answer

Gaston, I have a few suggestions / questions for you:

1 - Is this the code you are trying to execute?

 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 so, try commenting on this line: "Logger.getLogger (" org "). SetLevel (Level.ERROR);". Or change it to "Logger.getLogger (" org "). SetLevel (Level.ALL);". Then you should see errors on the output console.

2 - In your persistence.xml, I see that you are using the hsqldb database. Did you install / configure it correctly?

If you do not know this database, I suggest you use MySQL, PostgreSQL, or some database that you are familiar with.

3 - Check persistence.xml. I have a little bite:

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

Please note that the header has some XML declarations that may be important, as Hibernate tells you that your file is incorrect.

+2
source

All Articles