Thank you - the blueprints are very helpful.
I am still encountering a problem, however. The entity manager factory is not being injected via the @PersistenceUnit annotation. Without injection, the code works fine (ie: calling Persistence.createEntityManagerFactory() directly). Am I missing some configuration or dependency?
public class MyFacade implements ServletContextListener {
@PersistenceUnit(unitName = "myPu")
private EntityManagerFactory emf;
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
context.setAttribute("MyFacade", this);
}
public void contextDestroyed(ServletContextEvent sce) {
if (emf.isOpen()) {
emf.close();
}
}
public <T> T find(Class<T> entityClass, Object id) {
EntityManager em = emf.createEntityManager(); /* NullPointerException thrown here */
T entity = em.find(entityClass, id);
em.close();
return entity;
}
}
public class MyServlet extends HttpServlet {
protected MyFacade mf;
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
mf = (MyFacade) context.getAttribute("MyFacade");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
. . .
MyEntity myEntity = mf.find(MyEntity.class, id);
. . .
}
}
[Message sent by forum member 'shelleyb' (shelleyb)]
http://forums.java.net/jive/thread.jspa?messageID=239352