Questions tagged [entitymanager]

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

1676 questions
29
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,171
  • 2
  • 19
  • 43
29
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
  • 20,872
  • 14
  • 63
  • 85
28
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
  • 66,525
  • 139
  • 452
  • 886
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,929
  • 7
  • 33
  • 50
26
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
  • 16,125
  • 50
  • 122
  • 176
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,929
  • 7
  • 33
  • 50
25
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,216
  • 1
  • 11
  • 12
24
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
  • 1,924
  • 5
  • 26
  • 36
24
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
  • 4,159
  • 12
  • 49
  • 99
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
  • 4,997
  • 6
  • 33
  • 58
22
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,642
  • 5
  • 31
  • 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
  • 59,060
  • 84
  • 230
  • 322
19
votes
2 answers

Application vs Container Managed EntityManager

I am currently having a problem with understanding a concept of JPA. I am currently using/developing recent EclipseLink, Glassfish, Derby database to demonstrate a project. Before I develop something in much bigger picture, I need to be absolutely…
Hayati Guvence
  • 688
  • 2
  • 6
  • 22
19
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…
Yatendra
  • 31,339
  • 88
  • 211
  • 291
19
votes
4 answers

Does Guice Persist provide transaction scoped or application managed EntityManager?

We use Guice Persist to inject EntityManager in our project. E.g. public class MyDao{ @Inject EntityManager em; public void someMethod(){ //uses em instance } } But it is unclear for us how injected instance of EntityManager is…
Piotr Sobczyk
  • 6,015
  • 6
  • 42
  • 65