Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF selectOneMenu entity conversion fails with CDI converter
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/7022965/why-selectonemenu-send-itemlabel-to-the-converter">Why selectOneMenu Send ItemLabel to the converter?</a> </p> </blockquote> <p>I have a JSF page with two <code>p:selectOneMenu</code> on it which both represent pretty simple entities.</p> <p>ListType entity:</p> <pre><code>@Entity @Table( name = "LISTTYPES" ) @NamedQueries ( { @NamedQuery( name = "...", query = "SELECT lty " + "FROM ListType lty " + "WHERE lty.name = :name "), } ) public class ListType implements ClientDependent, Serializable { @Id @Column( name = "LISTTYPE_ID" ) private Long id; @Basic( optional = false ) @Column( name = "LIST_NO" ) private Long nbr; @Basic( optional = false ) @Column( name = "LIST_NAME" ) private String name; @Column( name = "LIST_DESC" ) private String description; ... } </code></pre> <p>MonitoringReason entity:</p> <pre><code>@Entity @Table( name = "MONITORINGREASONS" ) @NamedQueries ( { @NamedQuery( name = "...", query = "SELECT mtr " + "FROM MonitoringReason mtr " + "WHERE mtr.code = :code"), } ) public class MonitoringReason implements Serializable { @Id @Column( name = "MONITORINGREASON_ID" ) private Long id; @Basic( optional = false ) @Column( name = "MONITORINGREASON_NO" ) private String code; @Basic( optional = false ) @Column( name = "MONITORINGREASON_NAME" ) private String name; @Basic( optional = false ) @Column( name = "MONITORINGREASON_DESC" ) private String description; ... } </code></pre> <p>Simple entities. Note, I omitted the equals + hashCode methods, which both entities implement correctly.</p> <p>Here's some example data:</p> <p>List type data:</p> <pre><code>LISTTYPE_ID LIST_NO LIST_NAME LIST_DESC 1 3 'WL' 'Watch List' 0 4 'RL' 'Restricted List' 2 5 'Emb' 'Embargo' 7 7 'NRL' 'Not Recommended List' 5009 14 'GWL' 'Global Watch List' 5010 15 'GRL' 'Global Restricted List' 5011 16 'PIL' 'Permanent Insider List' </code></pre> <p>Monitoring reason data:</p> <pre><code>MONITORINGREASON_ID MONITORINGREASON_NO MONITORINGREASON_NAME MONITORINGREASON_DESC 3 'Ah' 'Ad-Hoc' 'Ad-Hoc' 6 'Al' 'Autom. Liste' 'Automatische Liste' 4 'Be' 'Beobachtung' 'Beobachtung' 7 'CLC' 'Limit Changes' 'Limit Changes' 1 'Fus' 'Fusion' 'Fusion' 5 'Li' 'Liste' 'Liste' 2 'Res' 'Research' 'Research Unternehmen' </code></pre> <p>The named queries on the entities above are for finding the entities by the converters:</p> <p>ListTypeConverter.java:</p> <pre><code>@Named @RequestScoped public class ListTypeConverter implements Converter { @Inject @SeamLogger private Logger log; @Inject private SessionHelper sessionHelper; @Inject private ListTypeService listTypeService; @Override public Object getAsObject( FacesContext ctx, UIComponent comp, String identifier ) { this.log.info( getClass().getSimpleName() + ".getAsObject: " + identifier ); if ( identifier == null ) { return null; } ListType listType = this.listTypeService.findByClientIdAndName( this.sessionHelper.getLoginUser().getClientId(), identifier ); this.log.info( "Returning " + listType.getName() + "!" ); return listType; } ... } </code></pre> <p>MonitoringReasonConverter.java:</p> <pre><code>@Named @RequestScoped public class MonitoringReasonConverter implements Converter { @Inject @SeamLogger private Logger log; @Inject private SessionHelper sessionHelper; @Inject private MonitoringReasonService monitoringReasonService; @Override public Object getAsObject( FacesContext ctx, UIComponent comp, String identifier ) { this.log.info( getClass().getSimpleName() + ".getAsObject: " + identifier ); if ( identifier == null ) { return null; } MonitoringReason monitoringReason = this.monitoringReasonService.findByClientIdAndCode( this.sessionHelper.getLoginUser().getClientId(), identifier ); this.log.info( "Returning " + monitoringReason.getName() + "!" ); return monitoringReason; } ... } </code></pre> <p>As you can see, two simple CDI converters, which are <strong>almost</strong> copies. The only difference is that list types are queried by name and monitoring reasons are queried by code. (see named queries, I checked that the correct ones are called by the services)</p> <p>They are used from a JSF page like:</p> <pre><code>&lt;p:selectOneMenu id="list-type" value="#{complianceCaseManager.selectedListType}" converter="#{listTypeConverter}"&gt; &lt;f:selectItems value="#{listTypeManager.selectableListTypes}" var="ltp" itemValue="#{ltp}" itemLabel="#{ltp.description}" /&gt; &lt;p:ajax process="@this" update="@form" /&gt; &lt;/p:selectOneMenu&gt; &lt;p:selectOneMenu id="monitoring-reason" value="#{complianceCaseManager.selectedMonitoringReason}" converter="#{monitoringReasonConverter}"&gt; &lt;f:selectItems value="#{monitoringReasonManager.selectableMonitoringReasons}" var="mtr" itemValue="#{mtr}" itemLabel="#{mtr.name}" /&gt; &lt;p:ajax process="@this" update="@form" /&gt; &lt;/p:selectOneMenu&gt; </code></pre> <p>OK, same here, basically the same copied code. The only difference is that list types use <code>itemLabel="#{ltp.description}"</code> and monitoring reasons use <code>itemLabel="#{mtr.name}"</code> as UI labels.</p> <p>What happens now is not what I had expected to happen:</p> <p>The list type <strong>and</strong> monitoring reason converters both get the name as identifier from JSF. This isn't a problem for the list type converter as it uses a query with the name to find the respective entity. This happens when executing a selection change:</p> <pre><code>2012-12-18 22:51:23,923 [http-thread-pool-8181(5)] INFO de.company.project.ListTypeConverter - ListTypeConverter.getAsObject: RL 2012-12-18 22:51:23,927 [http-thread-pool-8181(5)] INFO de.company.project.ListTypeConverter - Returning RL! </code></pre> <p>However, when using the monitoring reason select, the following is logged:</p> <pre><code>2012-12-18 22:52:02,091 [http-thread-pool-8181(2)] INFO de.company.project.MonitoringReasonConverter - MonitoringReasonConverter.getAsObject: Fusion 2012-12-18 22:52:02,100 [http-thread-pool-8181(2)] ERROR de.company.project.MonitoringReasonService - Monitoring reason not found for client ID = 1 with code Fusion javax.persistence.NoResultException: getSingleResult() did not retrieve any entities. at org.eclipse.persistence.internal.jpa.EJBQueryImpl.throwNoResultException(EJBQueryImpl.java:1307) at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:778) at de.company.project.MonitoringReasonService.findByClientIdAndCode(MonitoringReasonService.java:92) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:42) at sun.reflect.GeneratedMethodAccessor7832.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) at sun.reflect.GeneratedMethodAccessor7831.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) at $Proxy2626.findByClientIdAndCode(Unknown Source) at de.company.project.__EJB31_Generated__MonitoringReasonService__Intf____Bean__.findByClientIdAndCode(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:267) at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52) at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137) at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:263) at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:110) at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56) at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105) at de.company.project.MonitoringReasonService$Proxy$_$$_Weld$Proxy$.findByClientIdAndCode(MonitoringReasonService$Proxy$_$$_Weld$Proxy$.java) at de.company.project.MonitoringReasonConverter.getAsObject(MonitoringReasonConverter.java:63) at de.company.project.MonitoringReasonConverter$Proxy$_$$_WeldClientProxy.getAsObject(MonitoringReasonConverter$Proxy$_$$_WeldClientProxy.java) at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171) at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201) at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318) at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.getConvertedValue(SelectOneMenuRenderer.java:55) at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030) . . . </code></pre> <p><strong>Qs</strong>:</p> <ol> <li>Why is the name property taken to be used for <strong>both</strong> in the conversion process?</li> <li>What determines that the name property <em>should</em> be used for the getAsObject method string value and not - as I had always assumed - the property passed into <code>itemLabel="..."</code>? How do you get control??</li> <li>Where in the JSF spec is that information hidden? (if it's not PrimeFaces doing some overriding here)</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload