Questions tagged [entitymanager]

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

1802 questions
32
votes
6 answers

EntityManager refresh

I have web application using JPA. This entity manager keeps bunch of entites and suddenly I update the database from other side. I use MySQL and I use PhpMyAdmin and change some row. How to tell entity manager to re-synchronize, e.g. to forgot all…
zdenda.online
  • 2,451
  • 3
  • 23
  • 45
31
votes
4 answers

How to manually start a transaction on a shared EntityManager in Spring?

I have a LocalContainerEntityManagerFactoryBean as EntityManager instance. To quickly drop a full tables' content, I want to run the following code: @Service public class DatabaseService { @Autowired private EntityManager em; …
membersound
  • 81,582
  • 193
  • 585
  • 1,120
30
votes
3 answers

How frequently should I create an EntityManager?

I have an EntityManagerFactory for which I can create one (or multiple) EntityManager instances. I'm using a Servlet environment, and I've got one EntityManagerFactory wired up to the servlet (via the servlet context) which is shared for the…
AlBlue
  • 23,254
  • 14
  • 71
  • 91
28
votes
1 answer

Spring + Hibernate + JPA

As of now I have a working Spring application with persistence. However now I want to use Hibernate with JPA to do all of my database activities. I want to do this using an entitymanager. I've been reading many documents and tutorials on this…
Albinoswordfish
  • 1,949
  • 7
  • 34
  • 50
28
votes
4 answers

Getting a reference to EntityManager in Java EE applications using CDI

I'm using Java EE 7. I would like to know what is the proper way to inject a JPA EntityManager into an application scoped CDI bean. You can't just inject it using @PersistanceContext annotation, because EntityManager instances are not thread safe.…
Flying Dumpling
  • 1,294
  • 1
  • 11
  • 13
28
votes
5 answers

JAVA: an EntityManager object in a multithread environment

if I have multiple threads, each use injector to get the EntityManager object, each use the em object to select a list of other class objects. Ready to be used in a for loop. If a thread finishes first and calls clear(), will that affect the other…
user1589188
  • 5,316
  • 17
  • 67
  • 130
27
votes
1 answer

When should EntityManagerFactory instance be created/opened?

Ok, I read bunch of articles/examples how to write Entity Manager Factory in singleton. One of them easiest for me to understand a bit: http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html I learned that EntityManagerFactory…
Meow
  • 18,371
  • 52
  • 136
  • 180
25
votes
2 answers

Query a MySQL db using java

Guys, simply put, I have a java application with a text output box. I'd like to query a Db and display the output into a text box. Example I have a Db with two columns food and color I'd like to : SELECT * in Table WHERE color = 'blue' Any…
Lexicon
  • 2,467
  • 7
  • 33
  • 41
25
votes
5 answers

EntityManager refresh problem

I'm getting this error from my EntityManager when I call the refresh function. public void saveProduct(Product product) { entityManager.refresh(product); } I heard this could be a bug with Spring/Hibernate, however I'm not sure how to get…
Albinoswordfish
  • 1,949
  • 7
  • 34
  • 50
24
votes
2 answers

In Doctrine 2 can the Fetch Mode (Eager/Lazy etc.) be changed at runtime?

I have entities which I would like to eagerly load , and on other ocassions lazy (or even extra lazy) load. My mappings have no fetch mode declared in my YAML- so they use the default (lazy loading). Currently the only way to eagerly load is to by…
calumbrodie
  • 4,722
  • 5
  • 35
  • 63
24
votes
2 answers

What is exact purpose of flush in JPA

Some confusing explanation: flush(); Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.it will update or insert into your tables in the running transaction, but it may not commit those…
Nick Div
  • 5,338
  • 12
  • 65
  • 127
23
votes
3 answers

What is the best way to update the entity in JPA

I am doing some CRUD operations using JPA. For updating an object which is the right way to do? Through update query or through the find method of EntityManager? I have one Employee object I need to update. Which is the right way? Please guide me.
user414967
  • 5,225
  • 10
  • 40
  • 61
22
votes
2 answers

How to implement update () method in DAO using EntityManager (JPA)?

What is the standard way to implement simple update? Example: we have User with phone number NNNNNN and now we want to set it to YYYYYY. @PersistenceContext private EntityManager em; public void update (User transientUser) { what should be…
Roman
  • 64,384
  • 92
  • 238
  • 332
21
votes
4 answers

How to persist a lot of entities (JPA)

I need to process a CSV file and for each record (line) persist an entity. Right now, I do it this way: while ((line = reader.readNext()) != null) { Entity entity = createEntityObject(line); entityManager.save(entity); i++; } where the…
John Manak
  • 13,328
  • 29
  • 78
  • 119
21
votes
1 answer

When we need more than one EntityManager?

I am learning JPA and have one question: In which situations we need more than one EntityManager in our application? The two situations that I am aware of are as follows: When our application is a multi-threaded application and more than one thread…
Amit
  • 33,847
  • 91
  • 226
  • 299