In my Spring + JPA / Hibernate + Wicket application, I have a QueryBuilder bean that I want to use in one of my DAOs that generates a typed query using the criteria API:
@Service(value="inboxQueryBuilder")
public class InboxQueryBuilder {
@PersistenceContext
EntityManager em;
CriteriaBuilder cb;
public InboxQueryBuilder() {
cb = em.getCriteriaBuilder();
}
public TypedQuery<App> getQueryForApps(AppSearchObject aso) {
...
}
...
}
However, when I launch the application, I get a null pointer exception for the string:
cb = em.getCriteriaBuilder();
i.e. EntityManager is not entered. Do you know, why?
Also, is this used correctly and thread safe, or should I create an InboxQueryBuilder instance for each query? In this case, should I also enter an EntityManager or just pass it as a constructor parameter (will InboxQueryBuilder get an instance for every query in the DAO that has an injected EntityManager instance)?