createEntityManager throws java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus

If you’re using Hibernate 4 and trying to create a EntityManagerFactory manually (or SessionFactory) declared as <jta-datasource>, there is a property you must set on your persistence.xml called hibernate.transaction.jta.platform. For using on JBoss 7 <property name=”hibernate.transaction.jta.platform” value=”org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform” /> I found about this property at Hibernate Docs

How to set the timeout period on a JPA EntityManager query

Yes, there javax.persistence.query.timeout. According JPA 2.0 specification support for this query hint is optional: Portable applications should not rely on this hint. Depending on the persistence provider and database in use, the hint may or may not be observed. Default value (in milliseconds) can be set to persistence.xml for all queries: <property name=”javax.persistence.query.timeout” value=”1000″/> Same … Read more

When should EntityManagerFactory instance be created/opened?

EntityManagerFactory instances are heavyweight objects. Each factory might maintain a metadata cache, object state cache, EntityManager pool, connection pool, and more. If your application no longer needs an EntityManagerFactory, you should close it to free these resources. When an EntityManagerFactory closes, all EntityManagers from that factory, and by extension all entities managed by those EntityManagers, … Read more

The EntityManager is closed

My solution. Before doing anything check: if (!$this->entityManager->isOpen()) { $this->entityManager = $this->entityManager->create( $this->entityManager->getConnection(), $this->entityManager->getConfiguration() ); } All entities will be saved. But it is handy for particular class or some cases. If you have some services with injected entitymanager, it still be closed.

No Persistence provider for EntityManager named X

You must move persistence.xml file to an appropriate location. More specifically, add META-INF/persistence.xml file to the root of a source folder. In this case, the following is an appropriate location: src\main\java\META-INF\persistence.xml Here are the details: (taken from the JPA spec) A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF … Read more