The EntityManager is the representation of a PersistenceContext, allowing a user to manipulate data pulled from a database.
Questions tagged [entitymanager]
1802 questions
1054
votes
16 answers
JPA EntityManager: Why use persist() over merge()?
EntityManager.merge() can insert new objects and update existing ones.
Why would one want to use persist() (which can only create new objects)?

Aaron Digulla
- 321,842
- 108
- 597
- 820
118
votes
6 answers
@Autowired vs @PersistenceContext for EntityManager bean
What is the difference between:
@Autowired
private EntityManager em;
versus:
@PersistenceContext
private EntityManager em;
Both options work in my application, but can I break something by using the @Autowired annotation?

Cosmin Vasii
- 1,429
- 3
- 14
- 18
107
votes
21 answers
The EntityManager is closed
[Doctrine\ORM\ORMException]
The EntityManager is closed.
After I get a DBAL exception when inserting data, EntityManager closes and I'm not able to reconnect it.
I tried like this but it didn't get a…

Ueli
- 2,301
- 5
- 25
- 29
92
votes
6 answers
Create JPA EntityManager without persistence.xml configuration file
Is there a way to initialize the EntityManager without a persistence unit defined? Can you give all the required properties to create an entity manager? I need to create the EntityManager from the user's specified values at runtime. Updating the…

javydreamercsw
- 5,363
- 13
- 61
- 106
78
votes
5 answers
What does EntityManager.flush do and why do I need to use it?
I have an EJB where I am saving an object to the database. In an example I have seen, once this data is saved (EntityManager.persist) there is a call to EntityManager.flush(); Why do I need to do this? The object I am saving is not attached and…

spartikus
- 2,852
- 4
- 33
- 38
76
votes
5 answers
java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5
I have a java EE project using JPA (transaction-type="JTA"), hibernate as provider. I write my beans to handle the CRUD things. The program running in JBOSS 7 AS.
I have an EntityManagerDAO :
@Stateful
public class EntityManagerDao implements…

neptune
- 875
- 1
- 7
- 8
73
votes
4 answers
Injecting EntityManager Vs. EntityManagerFactory
A long question, please bear with me.
We are using Spring+JPA for a web application. My team is debating over injecting EntityManagerFactory in the GenericDAO (a DAO based on Generics something on the lines provided by APPFUSE, we do not use…

SB.
- 1,887
- 2
- 19
- 31
71
votes
1 answer
Symfony2: how to get all entities of one type which are marked with "EDIT" ACL permission?
Can someone tell me how to get all entities of one type which are marked with "EDIT" ACL permission?
I would like to build a query with the Doctrine EntityManager.

stoefln
- 14,498
- 18
- 79
- 138
68
votes
3 answers
JPA merge vs. persist
So far, my preference has been to always use EntityManager's merge() take care of both insert and update. But I have also noticed that merge performs an additional select queries before update/insert to ensure record does not already exists in the…

phewataal
- 1,107
- 4
- 12
- 23
57
votes
3 answers
PersistenceUnit vs PersistenceContext
In few project I have been successfully using
@PersistenceUnit(unitName = "MiddlewareJPA")
EntityManagerFactory emf;
...
EntityManager entityManager = emf.createEntityManager();
to obtain EntityManager for Database connection, but some days ago I…

user2377971
- 1,442
- 4
- 21
- 30
50
votes
7 answers
What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?
Can anybody explain what is the difference between the Spring Framework's LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

BlueSky
- 1,423
- 3
- 12
- 9
49
votes
3 answers
What is the difference between EntityManager.find() and EntityManger.getReference()?
Whats is the difference between
T EntityManager.find(Class entityClass, Object primaryKey) and
T EntityManager.getReference(Class entityClass, Object primaryKey)
?
I think getReference returns entity if it is managed.
and find…
user237673
35
votes
2 answers
Different ways of getting the EntityManager
The usual idiom I see for creating the EntityManager is something like this:
public class BaseDao {
private static final String PERSISTENCE_UNIT_NAME = "Employee";
EntityManagerFactory factory =…

jayjay
- 351
- 1
- 3
- 6
34
votes
3 answers
How to mock EntityManager?
I need to mock entity-manager to make testing service layer (in my case a session facade) to be independent of the underlying layer (which in my case is the entity-manager).
So how I can accomplish this? Should I use dbunit? Do I need easy/j(Mock)?

Muhammad Hewedy
- 29,102
- 44
- 127
- 219
34
votes
4 answers
Using EntityManager inside Doctrine 2.0 entities
I have 2 entities: Country (id, name) and Mapping (id, object, internalId, externalId).
Country and Mapping are not connected with associations (because Mapping has rows not only for country). I need to get external id for country using following…

Torchello
- 349
- 1
- 3
- 3