Cant use EntityManager into Converter
villo Apr 25, 2006 6:12 AMHi there
I'm experimenting a bit with seam, but I'm currently stuck on this issue...
I building a simple questionaire page; one has a list of questions with radio answers.
In order to display answers like:
<h:column>
<f:facet name="Answer">
<h:outputText value="Answers"/>
</f:facet>
<h:selectOneRadio value="#{question.selectedAnswer}" converter="#{ansbean.converter}">
<f:selectItems value="#{question.answerMap}" />
</h:selectOneRadio>
</h:column>
I had to write a converter bean, along the lines of the one in the dvd store example. This is basically
@Stateful
@Name("ansbean")
@Scope(ScopeType.APPLICATION)
@Interceptors(SeamInterceptor.class)
public class AnswersBean implements Answers {
Logger log = Logger.getLogger(Register.class);
List<Answer> answers;
Map<String, Answer> answersMap;
@PersistenceContext(type=EXTENDED)
EntityManager em;
@Create
public void loadData(){
//load some stuff from db
}
public Converter getConverter() {
return new AnswerConverter(em);
}
static public class AnswerConverter implements Converter, Serializable {
Logger log = Logger.getLogger(AnswerConverter.class);
private EntityManager em;
public AnswerConverter(EntityManager em) {
this.em = em;
}
public Object getAsObject(FacesContext facesContext,
UIComponent uiComponent,
String value) throws ConverterException {
if ((value == null) || value.length() == 0) {
return null;
}
log.info("INTO CONVERTER Asobject()");
int id = Integer.valueOf(value).intValue();
Answer ret = null;
ret = (Answer) em.createQuery("from Answer where id=?").setParameter(0,id).getSingleResult();
return ret;
}
public String getAsString(FacesContext facesContext,
UIComponent uiComponent,
Object object) throws ConverterException {
if (object == null) return null;
return String.valueOf(((Answer) object).getId());
}
}
}
In my Question entity bean (one to many relationship with Answer) I have
created an helper method like
@Transient
public Map<String, Answer> getAnswerMap(){
Map<String, Answer> results = new TreeMap<String, Answer>();
for (Answer answer : answers) {
result.put(answer.getText(), answer);
}
answerMap = results;
return answerMap;
}
The page loads the questionaire fine, but when I submit it I get the following trace:
11:32:32,734 ERROR [AnswersBean$AnswerConverter] java.lang.NullPointerException 11:32:32,734 ERROR [STDERR] java.lang.NullPointerException 11:32:32,735 ERROR [STDERR] at org.jboss.ejb3.entity.ExtendedEntityManager.getPersistenceContext(ExtendedEntityManager.java:59) 11:32:32,736 ERROR [STDERR] at org.jboss.ejb3.entity.ExtendedEntityManager.createQuery(ExtendedEntityManager.java:129) 11:32:32,736 ERROR [STDERR] at org.jboss.seam.example.registration.AnswersBean$AnswerConverter.getAsObject(AnswersBean.java:104) 11:32:32,736 ERROR [STDERR] at org.apache.myfaces.renderkit.RendererUtils.getConvertedUIOutputValue(RendererUtils.java:658) 11:32:32,736 ERROR [STDERR] at org.apache.myfaces.renderkit.html.HtmlRadioRendererBase.getConvertedValue(HtmlRadioRendererBase.java:288) 11:32:32,737 ERROR [STDERR] at javax.faces.component.UIInput.getConvertedValue(UIInput.java:289) 11:32:32,737 ERROR [STDERR] at javax.faces.component.UIInput.validate(UIInput.java:265) 11:32:32,737 ERROR [STDERR] at javax.faces.component.UIInput.processValidators(UIInput.java:144) 11:32:32,738 ERROR [STDERR] at javax.faces.component.UIData.process(UIData.java:514) 11:32:32,738 ERROR [STDERR] at javax.faces.component.UIData.processColumnChildren(UIData.java:498) 11:32:32,738 ERROR [STDERR] at javax.faces.component.UIData.processValidators(UIData.java:403) 11:32:32,738 ERROR [STDERR] at javax.faces.component.UIForm.processValidators(UIForm.java:68) 11:32:32,739 ERROR [STDERR] at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:417) 11:32:32,739 ERROR [STDERR] at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:142) 11:32:32,739 ERROR [STDERR] at org.apache.myfaces.lifecycle.LifecycleImpl.processValidations(LifecycleImpl.java:240) 11:32:32,740 ERROR [STDERR] at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76) 11:32:32,740 ERROR [STDERR] at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106) 11:32:32,740 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) 11:32:32,740 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 11:32:32,741 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 11:32:32,741 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) 11:32:32,741 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 11:32:32,742 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 11:32:32,742 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) 11:32:32,742 ERROR [STDERR] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:54) 11:32:32,743 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:174) 11:32:32,743 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74) 11:32:32,743 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) 11:32:32,744 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) 11:32:32,744 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) 11:32:32,744 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 11:32:32,745 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) 11:32:32,745 ERROR [STDERR] at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) 11:32:32,746 ERROR [STDERR] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) 11:32:32,746 ERROR [STDERR] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) 11:32:32,746 ERROR [STDERR] at java.lang.Thread.run(Unknown Source)
While the page that I'm trying to render contains:
<h:dataTable var="question" value="#{questions}" rendered="#{questions.rowCount>0}">
<h:column>
<f:facet name="Question">
<h:outputText value="Question text"/>
</f:facet>
<h:outputText value="#{question.text}" />
</h:column>
<h:column>
<f:facet name="Answer">
<h:outputText value="Answers"/>
</f:facet>
<h:outputText value="#{question.selectedAnswer}"/>
</h:column>
</h:dataTable>
Initially I had no EXTENDED PersistenceContextType but I had a similar NullPointerException in ManagedEntityManagerFactory.getNonTxEntityManager()
Does this have something to do with that on the second page there's no reference to ansbean?
Thanks in advance for any hint. I have to say that the seam model is somehow unclear to me yet...