What is the difference between EntityManager.find() and EntityManger.getReference()?

JPA has the concept of an EntityManager, as you know. During your work in the entity manager some objects are loaded from the database, can be modified and afterwards flushed to the database. find() has to return an initialized instance of your object. If it is not already loaded in the EntityManager, it is retrieved … Read more

What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

Basically JPA specification defines two types of entity managers. They are : i) Application-Managed : Application Managed entity manager means “Entity Managers are created and managed by merely the application ( i.e. our code )” . ii) Container Managed : Container Managed entity manager means “Entity Managers are created and managed by merely the J2EE … Read more

Symfony2: how to get all entities of one type which are marked with “EDIT” ACL permission?

I don’t believe there’s a default way of doing this. What you could do is to write your own service that adds a Filter to your Doctrine DQL queries. For more info, see: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/filters.html https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/dql-custom-walkers.html Hope this helps!

java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5

EntityManager#remove() works only on entities which are managed in the current transaction/context. In your case, you’re retrieving the entity in an earlier transaction, storing it in the HTTP session and then attempting to remove it in a different transaction/context. This just won’t work. You need to check if the entity is managed by EntityManager#contains() and … Read more

What does EntityManager.flush do and why do I need to use it?

A call to EntityManager.flush(); will force the data to be persisted in the database immediately as EntityManager.persist() will not (depending on how the EntityManager is configured: FlushModeType (AUTO or COMMIT) by default is set to AUTO and a flush will be done automatically. But if it’s set to COMMIT the persistence of the data to … 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

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.