In a stream that is generated by an external library, it is necessary to obtain data from the database. But when you try to access sessionFactory, a NullPointerException crashes. How to properly resolve this situation?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

In persistence.xml add the property " hibernate.session_factory_name " (if it is not there yet).

 <persistence-unit name="myJta"> <properties> <property name="hibernate.session_factory_name" value="java:/hibernate/MyFactory"/> </properties> </persistence-unit> 

In your code, do this:

 InitialContext ctx = new InitialContext(); sessionFactory = (SessionFactory) ctx.lookup(java:/hibernate/MyFactory); Session session = sessionFactory.openSession();