Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I'm trying to create a new EntityManager to persist my data, i get the following Error:

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

I using the JPA-Persistence Provider of Hibernate...

unluckily I couldn't find any helpful post in stackoverflow / with googling...

Does anybody have an idea where the cause of the error could lie?

Thank you for your help!

code-snippets:

creation of entitymanager

EntityManager em;
    EntityManagerFactory fact = Persistence.createEntityManagerFactory("bachelordebug"); // Here it crashes
    em = fact.createEntityManager();

stacktrace

javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:81)
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.uzh.platform.api.util.AssignmentUtil.findAll(AssignmentUtil.java:20)
at com.uzh.platform.api.services.GetAssignments.getAssignments(GetAssignments.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:402)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:156)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:233)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

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="bachelordebug" transaction-type="RESOURCE_LOCAL">
    <!-- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
   <class>com.uzh.platform.data.dao.Assignment</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/bachelor"/>
        <property name="javax.persistence.jdbc.user" value="bachelor"/>
        <property name="javax.persistence.jdbc.password" value="bachelor14"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.connection.release_mode" value="after_statement"/>
        <property name="connection.pool_size" value="1"/>
        <property name="dialect" value="org.postgresql.Driver"/>
        <property name="current_session_context_class" value="thread"/>
        <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        <property name="show_sql" value="true"/>
    </properties>
</persistence-unit>

like image 835
nicost Avatar asked Apr 08 '14 08:04

nicost


2 Answers

Since the error can indicate a lot of different causes it seems best to get to the cause examining the Hibernate DEBUG log statements (we use logback, but it should work in any other logging framework similarly):

With this you get a lot of output, but can determine the actually interesting classes (it is very likely you will spot the cause here already):

// (logback.groovy)

logger( "org.hibernate", DEBUG )

Which for us showed something like the following (where Mapping collection: foo.Bar.field -> some_tab_x was the info we needed to spot the faulty mapping):

07:32:43.720 D~ [main ~ main] [     :     ] CollectionSecondPass:SecondPass:  67|  
  Second pass for collection: foo.Bar.field
07:32:43.720 D~ [main ~ main] [     :     ] c.a.CollectionBinder:SecondPass: 823|  
  Binding a OneToMany: foo.Bar.field through a foreign key
07:32:43.720 D~ [main ~ main] [     :     ] c.a.CollectionBinder:SecondPass: 861|  
  Mapping collection: foo.Bar.field -> some_tab_x
07:32:43.727 D~ [main ~ main] [     :     ] ePersistenceProvider:gerFactory:  82|  
  Unable to build entity manager factory
07:32:43.743 E~ [main ~ main] [     :     ] .l.f.m.m.MgFooTstJu:AndRethrow:  74|  
  MyException: unit test error loading Foo with id: 4342153208-2: 
  <java.lang.NullPointerException> => 
  <javax.persistence.PersistenceException: Unable to build entity manager factory>

To restrict the logging again (if there are multiple places to fix your JPA mapping), we then used something like the following:

// (logback.groovy)

//logger( "org.hibernate", DEBUG )  // log everything

// infos on annotation mappping, e.g. getting NullPointerExceptions with 
//   javax.persistence.PersistenceException: Unable to build entity manager factory
logger( "org.hibernate.cfg.annotations.CollectionBinder", DEBUG )

(E.g. in Eclipse just use CTRL+SHIFT+T to search for the type you like to debug, if the log output does not show the complete classname)

like image 167
Andreas Covidiot Avatar answered Oct 18 '22 10:10

Andreas Covidiot


(Question answered in comments and edits. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

SOLVED

I finally found my error!

In my JPA-Entity class, I misspelled my Named-Query Annotation (even though I don't use it)

@NamedQuery(name="Assignment.findAll", query ="SELECT * FROM assignment")

of course one has to write that in capital Letters, i.e. "Assignment" (i.e. all terms like they appear in the service-class!)

3 days wasted with this stupid error - but now I learned how to do it! :-)

Thanks anyway for your help!

like image 2
3 revs, 2 users 94% Avatar answered Oct 18 '22 10:10

3 revs, 2 users 94%