0

In the following code, EntityManager is Injecting by Using CDI 2.0. This code throws,

java.lang.ClassNotFoundException: javax.persistence.EntityManager

@ApplicationScoped
public class PersonDao {

@Inject
private EntityManager entityManager;
/*
 * private EntityManagerFactory entityManagerFactory;
 */
public List<Person> getAllPersons()
{
    /*
     * entityManagerFactory=EntityManagerUtil.getEntityManagerFactory();
     * entityManager=entityManagerFactory.createEntityManager();
     */
    Query query=entityManager.createQuery("SELECT p FROM PersonEntity p");
    List<Person> persons=query.getResultList();
    return persons; 
}

}

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
saradhi
  • 1
  • 3
  • Uhhhh... classpath problems? – Kukeltje Dec 17 '19 at 07:12
  • @Kukeltje Can you please provide any solution with example – saradhi Dec 17 '19 at 08:19
  • 1
    i will presume that you're using CDI and JPA in EE environment, so to not confuse you or complicate things, please try to replace `@Inject` with `@PersistenceContext` and let me know if it will work. – whatamidoingwithmylife Dec 17 '19 at 08:43
  • @it'sBritneybitch: Do you mean that `@Inject` would throw a classnotfoundexception and `@PersistenceContext` would not? That would be weird.. (not saying you are wrong, just that it would be very weird if it made a difference) OP: How can I provide a solution and example for **your local classpath problems**. I would have hoped you have checked if this class is actually on your runtime classpath... – Kukeltje Dec 17 '19 at 09:03
  • still getting null pointer exception – saradhi Dec 17 '19 at 09:25
  • I reverted your edit since that is such a fundamental change in error that you must have done somethong that fixed the original error and yields the new one. So you effectively fixed the original problem which should be in answer. For the NPE create a new question bit make sure you read https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it upfront abd crwate a good question – Kukeltje Dec 26 '19 at 07:49
  • Reverted your edit again. Please create an **answer** how you solved the ClassNotFoundException and create a **new question** about the NPE you now get after fixing the original error. Chameleonizing questions is not good – Kukeltje Dec 27 '19 at 09:40
  • Reverted your edit again. Please create an **answer** how you solved the ClassNotFoundException and create a **new question** about the NPE you now get after fixing the original error. Chameleonizing questions is not good – Kukeltje Dec 31 '19 at 07:38
  • Most likely there was a typo in some configuration. OP created a new question as a successor of this without an answer to this.. https://stackoverflow.com/questions/59541849 – Kukeltje Dec 31 '19 at 09:37

1 Answers1

-2

Welcome to StackOverflow.

I need to make some assumptions about your situation. I'm going to assume that you are using Java EE in some capacity, and that your code is running in an application server. I'm pretty confident in this assumption, but it is certainly possible to have code like yours above that is deployed differently; if that is the case, then my answer may not apply.

Java EE had dependency injection before CDI came along. Well, rather, individual specifications in Java EE had their own flavors of dependency injection. The way that you inject an EntityManager in JPA when you are running on a Java EE application server is to use @PersistenceContext, like so:

@PersistenceContext
private EntityManager em;

Note the lack of @Inject, which didn't really enter the Java EE universe until well after JPA was already an established specification.

This injection recipe will cause a JTA transaction-scoped and synchronized EntityManager to be injected into your @ApplicationScoped-annotated bean here. The CDI bean housing it will be application scoped, but the EntityManager in question will not be.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
  • 1
    This does not explain `java.lang.ClassNotFoundException: javax.persistence.EntityManager`. This condition is impossible in a normal Java EE environment. So either OP is running it in Java SE context, or -more likely- OP is actually using a non-Java EE environment such as Tomcat or Jetty, and simply forgot to install JPA, or -more likely- installed it the wrong way (code would otherwise not compile in first place). – BalusC Dec 18 '19 at 18:26