@Autowired vs @PersistenceContext for EntityManager bean

You shouldn’t use @Autowired. @PersistenceContext takes care to create a unique EntityManager for every thread. In a production application you can have multiple clients calling your application in the same time. For each call, the application creates a thread. Each thread should use its own EntityManager. Imagine what would happen if they share the same … 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.

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

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

Create JPA EntityManager without persistence.xml configuration file

Is there a way to initialize the EntityManager without a persistence unit defined? You should define at least one persistence unit in the persistence.xml deployment descriptor. Can you give all the required properties to create an Entitymanager? The name attribute is required. The other attributes and elements are optional. (JPA specification). So this should be … Read more

JPA EntityManager: Why use persist() over merge()?

Either way will add an entity to a PersistenceContext, the difference is in what you do with the entity afterwards. Persist takes an entity instance, adds it to the context and makes that instance managed (i.e. future updates to the entity will be tracked). Merge returns the managed instance that the state was merged with. … Read more

tech