Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

java - Unable to build Hibernate SessionFactory exception from nowhere

I have a strange error appearing every time i want to deploy my application. I'm using IntelliJ IDEA 2016.3.

My application is Java WEB application using JSF, EJB and JPA (Hibernate). All libs were downloaded by IDEA (no Maven in this project), all dependencies are shipped with .war file. I'm deploying to TomEE 7.0.2 on Windows 10.

Here are my configuration files involved in deploying: resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <Resource id="my_internet_shop_db" type="javax.sql.DataSource">
        JdbcDriver  com.mysql.jdbc.Driver
        JdbcUrl jdbc:mysql://localhost:3306/my_internet_shop?autoReconnect=true&amp;useSSL=true
        UserName root
        Password alpine
        validationQuery = SELECT 1
        JtaManaged true
    </Resource>
</resources>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="my_internet_shop" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>my_internet_shop_db</jta-data-source>
        <class>Objects.Order.CartEntity</class>
        <class>Objects.User.ContactEntity</class>
        <class>Objects.Order.OrderEntity</class>
        <class>Objects.Product.ProductEntity</class>
        <class>Objects.Section.SectionEntity</class>
        <class>Objects.Service.ServiceEntity</class>
        <class>Objects.User.UserEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/my_internet_shop"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="alpine"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

And my stacktrace:

org.apache.openejb.OpenEJBException: org.apache.openejb.OpenEJBRuntimeException: javax.persistence.PersistenceException: [PersistenceUnit: my_internet_shop] Unable to build Hibernate SessionFactory: javax.persistence.PersistenceException: [PersistenceUnit: my_internet_shop] Unable to build Hibernate SessionFactory
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:872)
    ... 65 more
Caused by: org.apache.openejb.OpenEJBRuntimeException: javax.persistence.PersistenceException: [PersistenceUnit: my_internet_shop] Unable to build Hibernate SessionFactory
    at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createDelegate(ReloadableEntityManagerFactory.java:136)
    at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.<init>(ReloadableEntityManagerFactory.java:105)
    at org.apache.openejb.assembler.classic.PersistenceBuilder.createEntityManagerFactory(PersistenceBuilder.java:157)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:866)
    ... 65 more
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: my_internet_shop] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:951)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:151)
    at org.apache.openejb.assembler.classic.EntityManagerFactoryCallable.call(EntityManagerFactoryCallable.java:122)
    at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createDelegate(ReloadableEntityManagerFactory.java:134)
    ... 68 more
Caused by: org.hibernate.HibernateException: Could not access BeanManager ListenerFactory class [org.hibernate.jpa.event.internal.jpa.ListenerFactoryBeanManagerStandardImpl] to handle CDI extensions
    at org.hibernate.jpa.event.spi.jpa.ListenerFactoryBuilder.buildBeanManagerListenerFactory(ListenerFactoryBuilder.java:96)
    at org.hibernate.jpa.event.spi.jpa.ListenerFactoryBuilder.buildStandardBeanManagerListenerFactory(ListenerFactoryBuilder.java:59)
    at org.hibernate.jpa.event.spi.jpa.ListenerFactoryBuilder.buildListenerFactory(ListenerFactoryBuilder.java:42)
    at org.hibernate.jpa.event.spi.JpaIntegrator.integrate(JpaIntegrator.java:135)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:278)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:493)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:878)
    ... 71 more
Caused by: java.lang.IllegalStateException: On a thread without an initialized context nor a classloader mapping a deployed app
    at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(ThreadSingletonServiceImpl.java:287)
    at org.apache.openejb.cdi.ThreadSingletonServiceImpl.getContext(ThreadSingletonServiceImpl.java:263)
    at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(ThreadSingletonServiceImpl.java:298)
    at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(ThreadSingletonServiceImpl.java:60)
    at org.apache.webbeans.config.WebBeansFinder.getSingletonInstance(WebBeansFinder.java:51)
    at org.apache.webbeans.config.WebBeansContext.getInstance(WebBeansContext.java:185)
    at org.apache.webbeans.config.WebBeansContext.currentInstance(WebBeansContext.java:203)
    at org.apache.openejb.assembler.classic.EntityManagerFactoryCallable$1.findBm(EntityManagerFactoryCallable.java:105)
    at org.apache.openejb.assembler.classic.EntityManagerFactoryCallable$1.invoke(EntityManagerFactoryCallable.java:96)
    at com.sun.proxy.$Proxy70.toString(Unknown Source)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at org.hibernate.jpa.event.internal.jpa.ListenerFactoryBeanManagerStandardImpl.<init>(ListenerFactoryBeanManagerStandardImpl.java:60)
    at org.hibernate.jpa.event.internal.jpa.ListenerFactoryBeanManagerStandardImpl.fromBeanManagerReference(ListenerFactoryBeanManagerStandardImpl.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.jpa.event.spi.jpa.ListenerFactoryBuilder.buildBeanManagerListenerFactory(ListenerFactoryBuilder.java:80)
    ... 77 more

This is just a small part mentioning the exception. Here are the full stacktrace & server logs

All of the database stuff and JPA mapping are OK. Double-checked and refined to perfection. In fact, i was able to run my app on Tomcat 8.5.6 server, but it doesn't support EJB, so i need TomEE.

The root excpetion here is java.lang.IllegalStateException: On a thread without an initialized context nor a classloader mapping a deployed app. It appears in all of my Hibernate-JPA projects (even simple tests with only one entity and no space for error), so I believe this is the real server-side-configuration kind of problem.

However i wasn't able to find any clue about this exception neither on StackOverflow nor on the internet.

I'm looking forward for your help, would be glad to get any help and advice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can add in your persistence.xml

<property name="tomee.jpa.factory.lazy" value="true" />

or

<property name="tomee.jpa.cdi" value="false" />

if you don't need CDI/JPA integration

edit: first one will make the factory created on first use at runtime and second one just disable cdi. Both work cause you don't need CDI when JPA is initialized during startup (tomee creates the factory before starting cdi cause cdi can rely on it - chicken egg problem)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

57.0k users

...