Questions tagged [entitymanager]

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

1802 questions
13
votes
5 answers

Using flush() method on each 100 rows of 10 000 slows transaction

I have sample project using spring-boot with spring-data-jpa and postgres db with one table. I'm trying to INSERT 10 000 records in the loop into the table and measure execution time - enabling or disabling flush() method from EntityManager class…
user8280225
13
votes
2 answers

Why is persistence unit named null when persistence.xml exists

I am using Wildfly 8.1 together with a EJB Project (EJB 3.2) containing entities. When trying to inject the Entity Manager into one of my Beans i get the following: JBAS011440: Can't find a persistence unit named null in deployment…
Jbartmann
  • 1,459
  • 4
  • 24
  • 42
13
votes
1 answer

difference between 'detach' and 'remove' entityManager's methods

I would like to know what's the real difference between em.detach(entity), em.remove(entity) and using a JPQL request like : em.createQuery("DELETE FROM Country").exceuteUpdate(); thanks.
ThunderPhoenix
  • 1,649
  • 4
  • 20
  • 47
13
votes
1 answer

When using JPA entityManager why do you have to merge before you remove?

For a while now I have been wondering why when using JPA, do I have to write my delete methods like this: @Transactional public void delete(Account account) { if (entityManager.contains(account)) { entityManager.remove(account); …
testing123
  • 11,367
  • 10
  • 47
  • 61
13
votes
1 answer

@PersistenceContext EntityManager thread-safety in Spring and Java EE

EntityManager is not thread-safe by definition. Servlets specs says that in non-distributed environment and without implementing SingleThreadModel, there is only one servlet instance per definition. Therefore, in Java EE when you inject an…
Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
12
votes
3 answers

How should EntityManager be used in a nicely decoupled service layer and data access layer?

Somewhat related to my other question Should raw Hibernate annotated POJO's be returned from the Data Access Layer, or Interfaces instead? , I am experienced in creation of nicely decoupled layers, but not using Hibernate or J2EE/JPA. I have been…
Peter
  • 29,498
  • 21
  • 89
  • 122
12
votes
2 answers

createEntityManager throws java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus

I'm getting this stack trace when I deploy my hibernate app java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:72) [hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1] at…
user619804
  • 2,286
  • 12
  • 46
  • 71
12
votes
4 answers

Java /JPA | Query with specified inherited type

I am building a query on a generic table "Sample" and I have several types which inherit from this table "SampleOne", "SampleTwo". I require a query like : select s from Sample where s.type = :type where type would be a discriminator value of the…
redbull
  • 735
  • 3
  • 12
  • 21
12
votes
1 answer

How to inject EntityManager in CDI (weld)?

In my project , I use JSF+JPA+CDI+WildFly 8.2 in the persistence layer. I have a BasicDao , like this: public class BasicDao { private org.jboss.logging.Logger logger = org.jboss.logging.Logger .getLogger("BasicDao"); …
SuperChia
  • 131
  • 1
  • 1
  • 4
12
votes
6 answers

How to call Entity Manager in a constructor?

I've been trying to call Entity Manager in a constructor: function __construct() { $this->getDoctrine()->getEntityManager(); ... but, as I've seen in this answer: Stackoverflow question, it can't be done. So I wonder if there is a way to…
Manolo
  • 24,020
  • 20
  • 85
  • 130
12
votes
2 answers

Reusing entity manager by em.clear() or creating a new entity manager?

In my case of application managed transaction, I've to choose between: Using one single EntityManager and calling clear() before each new transaction. Share the EntityManager using a ThreadLocal. Creating a new EntityManager for each…
user2198754
  • 185
  • 3
  • 6
12
votes
3 answers

JPA EntityManager, how does it work?

Sorry for the noob question, but I'm having problems with JPA+Hibernate so I thought that something is not clear in my mind. I have some entities, say A, B, C, D and I have coded AMethods, BMethods, CMethods, DMethods. Each of the *Methods classes…
Raffo
  • 1,642
  • 6
  • 24
  • 41
12
votes
3 answers

Is there a way to pass detached object to JPA persist? (detached entity passed to persist)

I have 2 entities : Account and AccountRole. public class Account { private AccountRole accountRole; @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) public AccountRole getAccountRole() { return accountRole; …
Jaanus
  • 16,161
  • 49
  • 147
  • 202
11
votes
2 answers

Hibernate EntityManager, is it supposed to be used as a singleton?

I am not using Spring so I am creating an instance of EntityManager within a class. I used Hibernate-Eclipse reverse engineering to auto-generate the classes. These classes all has an instance of EntityManager. I am not 100% sure how Hibernate works…
user712850
11
votes
4 answers

Routing Read-Write transactions to Primary and Read_only transactions to Replicas using Spring and Hibernate

I have an application that uses Hibernate/JPA, with Spring and Jersey. In my application context I set the data source, define an entity manager factory, set the transaction manager with that entity manger factory, and have various service methods…
Elrond
  • 435
  • 1
  • 5
  • 14