Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:42:07+00:00 2026-05-26T07:42:07+00:00

Made a post earlier about this topic ( Display values in drill-down SelectOneMenus ).

  • 0

Made a post earlier about this topic (Display values in drill-down SelectOneMenus). The problem that have is not connected to the Object converters as I first thought.

I have two selectOneMenu depending on each other, Sector -> Category. When I persist the background business object that holds a Sector and Category, everything works as expected. Even after the current user session terminates and the user log on again and edit the previous saved business object.

The problem occur when and the entity manager flushes the objects and user wants to display the Sector, Category again from the database. The Sector selectOneMenu displays its value as it should, but the Category selectOneMenu value is not displayed. Although the backing bean has the correct value from the database.

What makes the selectOneMenuto not display certain persisted values/objects when they are loaded from database instead from in-memory?

Edit.xhtml

<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selected.category.sector}" title="#{bundle.CreateSectorLabel_sectorName}" valueChangeListener="#{activityController.changeSectorMenu}" immediate="true">
            <a4j:ajax event="change" execute="@this categoryMenu" render="categoryMenu"/>
            <f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>

<h:outputLabel value="Category:" />
    <h:selectOneMenu id="categoryMenu" value="#{activityController.selected.category}" title="#{bundle.CreateSectorLabel_sectorName}" 
        binding="#{activityController.categoryMenu}"
            required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}">                        
    <f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>

Controller bean for Category

@ManagedBean(name = "categoryController")
@SessionScoped
public class CategoryController implements Serializable{

    ....

    @FacesConverter(forClass = Category.class)
    public static class CategoryControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            CategoryController controller = (CategoryController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "categoryController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Category) {
                Category o = (Category) object;
                return getStringKey(o.getIdCategory());
            }
            else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CategoryController.class.getName());
            }
        }
    }

Part of POJO object

...
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idCategory")
    private Integer idCategory;
    ...

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Category)) {
            return false;
        }
        Category other = (Category) object;
        if ((this.idCategory == null && other.idCategory != null) || (this.idCategory != null && !this.idCategory.equals(other.idCategory))) {
            return false;
        }
        return true;
}

Functon for creating the SelectItem[] array

public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {

        int size = entities.size();
        SelectItem[] items;
        int i = 0;
        if (selectOne) {
            items = new SelectItem[size + 1];
            items[0] = new SelectItem("", ResourceBundle.getBundle("/resources/Bundle").getString("Select_item"));
            i++;
        } else {
            items = new SelectItem[size];
        }

        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:42:07+00:00Added an answer on May 26, 2026 at 7:42 am

    The problem was that I didn’t have the right collection of SelectItems when the user edited a business object. Plus that I added the <f:converter> to explicitly invoke the converter, where I did some changes in the getAsString() (the else part).

    Thank you for your time, and hope the post can be useful to someone else.

    SelectMenuOne
    
    public String prepareEditOrganisationActivity() {
            current = (Activity) organisationActivityItems.getRowData();
            Sector sector = current.getCategory().getSector();
            CategoryController categoryBean = JsfUtil.findBean("categoryController", CategoryController.class);
            categorySelection = categoryBean.categoryItemsBySector(sector);
    
            return "EditActivity";
    }
    

    CategoryControllerConverter

    ...    
        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Category) {
                Category o = (Category) object;
                return getStringKey(o.getIdCategory());
            }
            else {
                Category tmp = new Category();
                return getStringKey(tmp.getIdCategory());                
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made another post earlier about this subject but I've since changed my code
Recently, I made a post about the developers I'm working with not using try
Since I'm sure many people have different standard, I've made this post a community
Reading this blog post about HttpOnly cookies made me start thinking, is it possible
Follow up on this post I made earlier on. I found out XML actually
I have aa question re:AS 3. I made a Post-It note movie clip that
I have made this post over a year ago, and I think it makes
I've made a previous post that tried to use a textbox. From this I
I made a post about this yesterday, but it is a fairly different question.
Post-release, I have made one small change to one form in our development site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.