EntityManager injection - NullPointerException
You can't access the EntityManager within the constructor. Take a look at the @PostConstruct-Annotation
@Service(value="inboxQueryBuilder")
public class InboxQueryBuilder {
@PersistenceContext
EntityManager em;
CriteriaBuilder cb;
public InboxQueryBuilder() {
// em= null
}
@PostConstruct
public void toSomething(){
// em set by Container
cb = em.getCriteriaBuilder();
}
public TypedQuery<App> getQueryForApps(AppSearchObject aso) {
...
}
...
}
EDIT:
After reading your post again, I start to became unsure, if I'm right. I know the Java EE-Dependency-Injection within a JBoss works as I described, but I'm not sure about spring-IOC.
PersistenceContext EntityManager injection NullPointerException
An entity manager can only be injected in classes running inside a transaction. In other words, it can only be injected in a EJB. Other classe must use an EntityManagerFactory to create and destroy an EntityManager.
Since your TestService is not an EJB, the annotation @PersistenceContext is simply ignored. Not only that, in JavaEE 5, it's not possible to inject an EntityManager nor an EntityManagerFactory in a JAX-RS Service. You have to go with a JavaEE 6 server (JBoss 6, Glassfish 3, etc).
Here's an example of injecting an EntityManagerFactory:
package com.test.service;
import java.util.*;
import javax.persistence.*;
import javax.ws.rs.*;
@Path("/service")
public class TestService {
@PersistenceUnit(unitName = "test")
private EntityManagerFactory entityManagerFactory;
@GET
@Path("/get")
@Produces("application/json")
public List get() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
return entityManager.createQuery("from TestEntity").getResultList();
} finally {
entityManager.close();
}
}
}
The easiest way to go here is to declare your service as a EJB 3.1, assuming you're using a JavaEE 6 server.
Related question:
Inject an EJB into JAX-RS (RESTful service)
Injecting EntityManager using Spring ( Null Pointer Exception )
Since you are instantiating TeacherDaoImpl
yourself (with the new
keyword) within main, Spring is not injecting the EntityManager
and hence the NPE.
Annotate the field TeacherDaoImpl.entityManager
with @PersistenceContext
and annotate the TeacherDaoImpl
class with @Component
to have Spring instantiate it for you. Then in your main, get a hold of that bean:
TeacherDao dao = applicationContext.getBean(TeacherDao.class);
// ...
Also these two directives seem to be unnecessary:
<context:annotation-config />
<context:spring-configured />
The former is implied when you are using <context:component-scan />
. The latter is only useful if you are using @Configurable
in your code.
EJB @PersistenceContext EntityManager Throws NullPointerException
The EntityManager instance, is injected when the EJB is deployed in the Container.
If you take a look at the lifecycle of enterprise bean, you will see clearly when dependency injection occurs.
When the Container sees the @Persistencecontext annotation it will inject a container-managed EntityManager.
The problem is that the code executed in this test is not managed by the Container, therefore, no one inject the necessary dependencies.
bookDao = new BookEJB();
When you run the test, the BookEJB class is just a simple POJO, the @Stateless and @PersistenceContext annotations are simply ignored.
You have several alternatives in order to test your EJB, take a look at this link.
Related Topics
How to Calculate a Time Span in Java and Format the Output
Jtable Model Listener Detects Inserted Rows Too Soon (Before They Are Drawn)
Bytebuffer.Allocate() VS. Bytebuffer.Allocatedirect()
Hadoop No Filesystem for Scheme: File
Java Garbage Collector - When Does It Collect
How to Remove Duplicate White Spaces in String Using Java
Retrieving the Inherited Attribute Names/Values Using Java Reflection
Java 8 Lambda Expressions - What About Multiple Methods in Nested Class
Java Array Hashcode Implementation
Read Whole Text Files from a Compression in Spark
Autocomplete with Java , Redis, Elastic Search , Mongo
Naming Threads and Thread-Pools of Executorservice
What Is More Efficient: System.Arraycopy or Arrays.Copyof
When Should I Use an Interface in Java
Java Conditional Compilation: How to Prevent Code Chunks from Being Compiled
Use of Class Definitions Inside a Method in Java
Java8: Why Is It Forbidden to Define a Default Method for a Method from Java.Lang.Object