0

Possible Duplicate:
Why selectOneMenu Send ItemLabel to the converter?

I have a JSF page with two p:selectOneMenu on it which both represent pretty simple entities.

ListType entity:

@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;

    ...
}

MonitoringReason entity:

@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;

    ...
}

Simple entities. Note, I omitted the equals + hashCode methods, which both entities implement correctly.

Here's some example data:

List type data:

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'

Monitoring reason data:

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'

The named queries on the entities above are for finding the entities by the converters:

ListTypeConverter.java:

@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;
    }

    ...
}

MonitoringReasonConverter.java:

@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;
    }

    ...
}

As you can see, two simple CDI converters, which are almost 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)

They are used from a JSF page like:

<p:selectOneMenu id="list-type"
                 value="#{complianceCaseManager.selectedListType}"
                 converter="#{listTypeConverter}">
    <f:selectItems value="#{listTypeManager.selectableListTypes}"
                   var="ltp"
                   itemValue="#{ltp}"
                   itemLabel="#{ltp.description}" />
    <p:ajax process="@this" update="@form" />
</p:selectOneMenu>

<p:selectOneMenu id="monitoring-reason"
                 value="#{complianceCaseManager.selectedMonitoringReason}"
                 converter="#{monitoringReasonConverter}">
    <f:selectItems value="#{monitoringReasonManager.selectableMonitoringReasons}"
                   var="mtr"
                   itemValue="#{mtr}"
                   itemLabel="#{mtr.name}" />
    <p:ajax process="@this" update="@form" />
</p:selectOneMenu>

OK, same here, basically the same copied code. The only difference is that list types use itemLabel="#{ltp.description}" and monitoring reasons use itemLabel="#{mtr.name}" as UI labels.

What happens now is not what I had expected to happen:

The list type and 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:

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!

However, when using the monitoring reason select, the following is logged:

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)
    .
    .
    .

Qs:

  1. Why is the name property taken to be used for both in the conversion process?
  2. What determines that the name property should be used for the getAsObject method string value and not - as I had always assumed - the property passed into itemLabel="..."? How do you get control??
  3. Where in the JSF spec is that information hidden? (if it's not PrimeFaces doing some overriding here)
Community
  • 1
  • 1
Kawu
  • 13,647
  • 34
  • 123
  • 195

2 Answers2

1

I think this will be the problem of Converter. I found a question similar to this, check this it may help.Why selectOneMenu Send ItemLabel to the converter?

Community
  • 1
  • 1
Abin Manathoor Devasia
  • 1,945
  • 2
  • 21
  • 47
0

The answer is ultimately that my converter's getAsString() method defines the strings that getAsObject receives. I had always assumed that the value is determined by JSF, e.g. the itemLabel="...", which isn't the case.

I kind of makes sense this way when thinking about it, but I'm rather shocked that I have missed this information for my 1.5+ years of reading and programming JSF...

Kawu
  • 13,647
  • 34
  • 123
  • 195