javax.persistence.PersistenceException: Unable to build entity manager factory

Hibernate JIRA | Luke Woodward | 2 years ago
  1. 0

    The following annotated entity classes cause a NullPointerException to be thrown when creating an EntityManagerFactory: {code} package com.example; import javax.persistence.*; import java.util.Set; @Entity @Table(name = "A") public class A { @Id @GeneratedValue // @Column(name = "aid") // private int aid; // @OneToMany @JoinColumn(name = "aid") private Set<B> bs; } {code} {code} package com.example; import javax.persistence.*; @Entity @Table(name = "B") public class B { @Id @GeneratedValue @Column(name = "bid") private int bid; @ManyToOne @JoinColumn(name = "aid") private A a; } {code} The JPA persistence unit is defined as follows: {code:xml|title=META-INF/persistence.xml} <?xml version="1.0"?> <persistence 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" version="2.0"> <persistence-unit name="example"> <class>com.example.A</class> <class>com.example.B</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> </properties> </persistence-unit> </persistence> {code} When I run the following class {code} package com.example; public class Main { public static void main(String[] args) { javax.persistence.Persistence.createEntityManagerFactory("example"); } } {code} I get the following stacktrace written to the output: {noformat} Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) at com.example.Main.main(Main.java:5) Caused by: java.lang.NullPointerException at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:550) at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258) at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116) at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1596) at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1519) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) ... 4 more {noformat} The persistence unit is intentionally missing most of the database configuration. The NPE is reproducible with only the dialect, so I left out the rest of it in order to make the test case minimal. It makes no difference if you do add back the missing configuration, you still get an NPE. So it seems that the NPE is being thrown before Hibernate talks to the database. In this situation I would much rather there was an exception thrown with a message explaining to me that I'm not using the annotations correctly. The NPE is not remotely helpful and goes no way at all to help a Hibernate novice find and fix their problem. In the above case, the fix is to uncomment the commented-out lines in class A. Once you do that, and restore the missing properties to the persistence unit, the NPE goes away and Hibernate works as expected. This issue arose from a [Stack Overflow question|http://stackoverflow.com/questions/25698850/].

    Hibernate JIRA | 2 years ago | Luke Woodward
    javax.persistence.PersistenceException: Unable to build entity manager factory
  2. 0

    The following annotated entity classes cause a NullPointerException to be thrown when creating an EntityManagerFactory: {code} package com.example; import javax.persistence.*; import java.util.Set; @Entity @Table(name = "A") public class A { @Id @GeneratedValue // @Column(name = "aid") // private int aid; // @OneToMany @JoinColumn(name = "aid") private Set<B> bs; } {code} {code} package com.example; import javax.persistence.*; @Entity @Table(name = "B") public class B { @Id @GeneratedValue @Column(name = "bid") private int bid; @ManyToOne @JoinColumn(name = "aid") private A a; } {code} The JPA persistence unit is defined as follows: {code:xml|title=META-INF/persistence.xml} <?xml version="1.0"?> <persistence 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" version="2.0"> <persistence-unit name="example"> <class>com.example.A</class> <class>com.example.B</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> </properties> </persistence-unit> </persistence> {code} When I run the following class {code} package com.example; public class Main { public static void main(String[] args) { javax.persistence.Persistence.createEntityManagerFactory("example"); } } {code} I get the following stacktrace written to the output: {noformat} Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) at com.example.Main.main(Main.java:5) Caused by: java.lang.NullPointerException at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:550) at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258) at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116) at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1596) at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1519) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) ... 4 more {noformat} The persistence unit is intentionally missing most of the database configuration. The NPE is reproducible with only the dialect, so I left out the rest of it in order to make the test case minimal. It makes no difference if you do add back the missing configuration, you still get an NPE. So it seems that the NPE is being thrown before Hibernate talks to the database. In this situation I would much rather there was an exception thrown with a message explaining to me that I'm not using the annotations correctly. The NPE is not remotely helpful and goes no way at all to help a Hibernate novice find and fix their problem. In the above case, the fix is to uncomment the commented-out lines in class A. Once you do that, and restore the missing properties to the persistence unit, the NPE goes away and Hibernate works as expected. This issue arose from a [Stack Overflow question|http://stackoverflow.com/questions/25698850/].

    Hibernate JIRA | 2 years ago | Luke Woodward
    javax.persistence.PersistenceException: Unable to build entity manager factory
  3. Speed up your debug routine!

    Automated exception search integrated into your IDE

  1. Kawada 54 times, last 7 months ago
15 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.NullPointerException

    No message provided

    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType()
  2. Hibernate
    Configuration.buildSessionFactory
    1. org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:550)
    2. org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258)
    3. org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
    4. org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1596)
    5. org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1519)
    6. org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420)
    7. org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    7 frames
  3. org.hibernate.jpa
    EntityManagerFactoryBuilderImpl$4.perform
    1. org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    2. org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
    2 frames
  4. org.hibernate.boot
    ClassLoaderServiceImpl.withTccl
    1. org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
    1 frame
  5. org.hibernate.jpa
    HibernatePersistenceProvider.createEntityManagerFactory
    1. org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    2. org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
    2 frames
  6. Hibernate EJB
    HibernatePersistence.createEntityManagerFactory
    1. org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    1 frame
  7. JavaEE 7
    Persistence.createEntityManagerFactory
    1. javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    2. javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    2 frames
  8. com.example
    Main.main
    1. com.example.Main.main(Main.java:5)
    1 frame