Hi Friends,
I am writing a custom tag as shown below. I am using JPA. I am trying to get a reference to my entityManager but it keeps coming up null!
I am referencing the EntityManager in my controllers the same way and it is working fine. This is how I tried to get a reference to the entityManager in my controllers:
I did some more digging and I found that the EntityManager can be injected in my custom tag according to this definition for WebSphere, although I am using Tomcat:Code:@PersistenceContext public void setEntityManager(EntityManager entityManager) { this. entityManager = entityManager; }
The following is the code for my custom tag (note I did try to use @PersistenceContext but it did not work):Code:Attention: Injection of the EntityManager is only supported for the following artifacts: * EJB 3.0 session beans * EJB 3.0 message-driven beans * Servlets, Servlet Filters, and Listeners * JSP tag handlers which implement interfaces javax.servlet. jsp.tagext.Tag and javax.servlet.jsp.tagext.SimpleTag * Java Server Faces (JSF) managed beans * the main class of the application client.
How would I get a reference to entityManager in this case??Code:public class CommentsTag implements Tag { private static final long serialVersionUID = -1395963113880536744L; private EntityManager entityManager; private JspWriter out; private PageContext pageContext; //@PersistenceContext(unitName = "dataAccessPU") @PersistenceContext public void setEntityManager(EntityManager entityManager) { this. entityManager = entityManager; } public int doStartTag() throws JspException { // TODO Auto-generated method stub // Get topic!! String sql_query_tod = "SELECT * FROM topic t WHERE t.type='tod' AND t.status='active';"; Query queryTOD = (Query) entityManager.createNativeQuery(sql_query_tod,Topic.class);
Please advise. Thank you.


Reply With Quote

