javax.persistence.PersistenceException: [PersistenceUnit: TestAuditPU] Unable to build EntityManagerFactory

Hibernate JIRA | Rino Heinen | 7 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Hi, there is a problem when a class is @Audited and this has an @EmbeddedId (only when the Embeddable class has ManyToOne relationship). Example code: persistence.xml: <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.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_1_0.xsd"> <persistence-unit name="TestAuditPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:mysql://192.168.0.6:3306/idm"/> <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener" /> </properties> </persistence-unit> </persistence> Class A: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testaudit; import java.io.Serializable; import javax.persistence.*; import org.hibernate.envers.Audited; /** * * @author heir */ @Entity @Table(name="tblA") @Audited public class A implements Serializable { @Id private String name; @OneToMany(mappedBy="bpk.a") private java.util.List<B> bs; public A() { } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the bs */ public java.util.List<B> getBs() { return bs; } /** * @param bs the bs to set */ public void setBs(java.util.List<B> bs) { this.bs = bs; } } Class B: @Entity @Table(name="tblB") @Audited public class B implements Serializable { @Id private BPK bpk; public B() { } /** * @return the bpk */ public BPK getBpk() { return bpk; } /** * @param bpk the bpk to set */ public void setBpk(BPK bpk) { this.bpk = bpk; } } Class BPK: @Embeddable public class BPK implements Serializable { private String name; @ManyToOne(targetEntity=testaudit.A.class, cascade={CascadeType.ALL}) @JoinColumn(name="strAName") private A a; public BPK() { } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the a */ public A getA() { return a; } /** * @param a the a to set */ public void setA(A a) { this.a = a; } } Exception: Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: TestAuditPU] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) at testaudit.Main.main(Main.java:23) Caused by: org.hibernate.HibernateException: could not init listeners at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205) at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:891) ... 4 more Caused by: org.hibernate.MappingException: Type not supported: org.hibernate.type.ManyToOneType at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addIdProperties(IdMetadataGenerator.java:75) at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addId(IdMetadataGenerator.java:120) at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:374) at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:100) at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:86) at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:99) at org.hibernate.envers.event.AuditEventListener.initialize(AuditEventListener.java:334) at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198) at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181) at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194) ... 8 more Java Result: 1 Thanks Heinen Rino

    Hibernate JIRA | 7 years ago | Rino Heinen
    javax.persistence.PersistenceException: [PersistenceUnit: TestAuditPU] Unable to build EntityManagerFactory
  2. 0

    Hi, there is a problem when a class is @Audited and this has an @EmbeddedId (only when the Embeddable class has ManyToOne relationship). Example code: persistence.xml: <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.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_1_0.xsd"> <persistence-unit name="TestAuditPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:mysql://192.168.0.6:3306/idm"/> <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener" /> <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener" /> </properties> </persistence-unit> </persistence> Class A: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testaudit; import java.io.Serializable; import javax.persistence.*; import org.hibernate.envers.Audited; /** * * @author heir */ @Entity @Table(name="tblA") @Audited public class A implements Serializable { @Id private String name; @OneToMany(mappedBy="bpk.a") private java.util.List<B> bs; public A() { } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the bs */ public java.util.List<B> getBs() { return bs; } /** * @param bs the bs to set */ public void setBs(java.util.List<B> bs) { this.bs = bs; } } Class B: @Entity @Table(name="tblB") @Audited public class B implements Serializable { @Id private BPK bpk; public B() { } /** * @return the bpk */ public BPK getBpk() { return bpk; } /** * @param bpk the bpk to set */ public void setBpk(BPK bpk) { this.bpk = bpk; } } Class BPK: @Embeddable public class BPK implements Serializable { private String name; @ManyToOne(targetEntity=testaudit.A.class, cascade={CascadeType.ALL}) @JoinColumn(name="strAName") private A a; public BPK() { } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the a */ public A getA() { return a; } /** * @param a the a to set */ public void setA(A a) { this.a = a; } } Exception: Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: TestAuditPU] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) at testaudit.Main.main(Main.java:23) Caused by: org.hibernate.HibernateException: could not init listeners at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205) at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:891) ... 4 more Caused by: org.hibernate.MappingException: Type not supported: org.hibernate.type.ManyToOneType at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addIdProperties(IdMetadataGenerator.java:75) at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addId(IdMetadataGenerator.java:120) at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:374) at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:100) at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:86) at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:99) at org.hibernate.envers.event.AuditEventListener.initialize(AuditEventListener.java:334) at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198) at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181) at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194) ... 8 more Java Result: 1 Thanks Heinen Rino

    Hibernate JIRA | 7 years ago | Rino Heinen
    javax.persistence.PersistenceException: [PersistenceUnit: TestAuditPU] Unable to build EntityManagerFactory
  3. Speed up your debug routine!

    Automated exception search integrated into your IDE

  4. 0

    Since the resolution of HHH-3563, relations in components are now supported. However, if I try to use a @NotAudited @ManyToOne in an @Embeddable, then Envers does not start up. Removing @NotAudited to the @ManyToOne @Embeddable and adding @Audited to the related entity resolves the issue. This does not occur with @NotAudited @ManyToOne in an @Entity. See attached test case to reproduce this issue. Stack trace: javax.persistence.PersistenceException: Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677) at org.hibernate.envers.test.AbstractEntityTest.init(AbstractEntityTest.java:94) at org.hibernate.envers.test.AbstractEntityTest.init(AbstractEntityTest.java:82) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:398) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:145) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:82) at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104) at org.testng.TestRunner.runWorkers(TestRunner.java:712) at org.testng.TestRunner.privateRun(TestRunner.java:582) at org.testng.TestRunner.run(TestRunner.java:477) at org.testng.SuiteRunner.runTest(SuiteRunner.java:324) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292) at org.testng.SuiteRunner.run(SuiteRunner.java:198) at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823) at org.testng.TestNG.runSuitesLocally(TestNG.java:790) at org.testng.TestNG.run(TestNG.java:708) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124) Caused by: org.hibernate.HibernateException: could not init listeners at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205) at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1352) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) ... 24 more Caused by: org.hibernate.MappingException: An audited relation to a non-audited entity org.hibernate.envers.test.entities.UnversionedStrTestEntity! at org.hibernate.envers.configuration.metadata.ToOneRelationMetadataGenerator.addToOne(ToOneRelationMetadataGenerator.java:59) at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:126) at org.hibernate.envers.configuration.metadata.ComponentMetadataGenerator.addComponent(ComponentMetadataGenerator.java:40) at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:121) at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addProperties(AuditMetadataGenerator.java:161) at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateSecondPass(AuditMetadataGenerator.java:348) at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:96) at org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:87) at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:99) at org.hibernate.envers.event.AuditEventListener.initialize(AuditEventListener.java:232) at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198) at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181) at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194) ... 28 more

    Hibernate JIRA | 8 years ago | Erik-Berndt Scheper
    javax.persistence.PersistenceException: Unable to build EntityManagerFactory

    Root Cause Analysis

    1. org.hibernate.MappingException

      Type not supported: org.hibernate.type.ManyToOneType

      at org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addIdProperties()
    2. A Hibernate O/RM Module
      AuditEventListener.initialize
      1. org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addIdProperties(IdMetadataGenerator.java:75)
      2. org.hibernate.envers.configuration.metadata.IdMetadataGenerator.addId(IdMetadataGenerator.java:120)
      3. org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:374)
      4. org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:100)
      5. org.hibernate.envers.configuration.AuditConfiguration.<init>(AuditConfiguration.java:86)
      6. org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:99)
      7. org.hibernate.envers.event.AuditEventListener.initialize(AuditEventListener.java:334)
      7 frames
    3. Hibernate
      EventListeners.initializeListeners
      1. org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)
      2. org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)
      3. org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)
      3 frames
    4. Hibernate
      AnnotationConfiguration.buildSessionFactory
      1. org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396)
      2. org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
      3. org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
      3 frames
    5. Hibernate EJB
      HibernatePersistence.createEntityManagerFactory
      1. org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:891)
      2. org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
      2 frames
    6. JavaEE 7
      Persistence.createEntityManagerFactory
      1. javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
      2. javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
      2 frames
    7. testaudit
      Main.main
      1. testaudit.Main.main(Main.java:23)
      1 frame