Questions tagged [entitymanager]

The EntityManager is the representation of a PersistenceContext, allowing a user to manipulate data pulled from a database.

1676 questions
992
votes
15 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
  • 297,790
  • 101
  • 558
  • 777
88
votes
19 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,041
  • 4
  • 24
  • 29
82
votes
7 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,060
  • 13
  • 56
  • 97
72
votes
5 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
  • 969
  • 2
  • 10
  • 17
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
  • 13,576
  • 16
  • 74
  • 131
71
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,827
  • 1
  • 19
  • 30
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,067
  • 3
  • 12
  • 20
67
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,412
  • 3
  • 27
  • 36
62
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
  • 725
  • 1
  • 6
  • 8
53
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,362
  • 3
  • 20
  • 29
48
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
41
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,333
  • 3
  • 11
  • 9
34
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
  • 341
  • 1
  • 3
  • 5
33
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
  • 339
  • 1
  • 3
  • 3
31
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
  • 26,344
  • 42
  • 116
  • 201
1
2 3
99 100