0

Some help would be appreciated on this issue.

I have several JSF Pages, but one of them is a Subcategory where there is a SelectOneMenu for choosing the Category, but when I try to edit the subcategory, this SelectOneMenu always shows the first value and is not getting preselected.

How could I solve this. I have read a lot of SO posts and eventhougn I have implemented a couple of advice I have not achieved it. for example, a @BalusC's one:

primefaces selectOneMenu doesn't working when it should

Here is the View

<h:outputText value="Subcategory ID : #{subcategoryController.subcategory.subcategoryId}"/>

<p:selectOneMenu id="cboCategoryDialog" converter="subcategoryConverter"
                 value="#{subcategoryController.category.categoryId}"
                 style="width: 100%"

    <f:selectItems value="#{subcategoryController.categoryList}"
                   var="subcat"
                   itemLabel="#{subcat.categoryName}"
                   itemValue="#{subcat.categoryId}"/>
</p:selectOneMenu>**

This is the Subcategory Entity:

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "subcategory_id")
private Integer subcategoryId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "subcategory_name")
private String subcategoryName;
@JoinColumn(name = "category_id", referencedColumnName = "category_id")
@OneToOne(optional = false;
private Category categoryId;

//Getters and Setters Methods

@Override
public int hashCode() {
    return (subcategoryId != null)
            ? (this.getClass().hashCode() + subcategoryId.hashCode())
            : super.hashCode();
}

@Override
public boolean equals(Object obj) {

    return (obj instanceof Subcategory) && (subcategoryId != null)
            ? subcategoryId.equals(((Subcategory) obj).subcategoryId)
            : (obj == this);
}

SubcategoryConverter

@RequestScoped
@FacesConverter("subcategoryConverter")
public class SubcategoryConverter implements Converter {

@EJB
private SubcategoryFacadeLocal EJBsubcategory;

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (!(value instanceof Subcategory) || ((Subcategory) value).getSubcategoryId() == null) {
        return null;
    }

    return String.valueOf(((Subcategory) value).getSubcategoryId());
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || !value.matches("\\d+")) {
        return null;
    }

    Subcategory subcategory = EJBsubcategory.find(Integer.valueOf(value));

    if (subcategory == null) {
        throw new ConverterException(new FacesMessage("Unknown operation ID: " + value));
    }

    return subcategory;

    }
}

Below image shows the Dialog when I try to edit any item, it always shows computer because its the first itm.

See the image showing the issue

I have been reading some related useful questions about this, but I have not been able to fix it.

Thanks in advance,

Community
  • 1
  • 1
JVz
  • 41
  • 2
  • 6
  • 1
    where is your converter ? what's subcategoryController.category.categoryId ? – Kenshin Oct 09 '16 at 05:16
  • 1
    I'm using Onmifaces SelectItemsConverter. subcategoryController.category.categoryId is where the selected category is saved in subcategory entity. @Arun – JVz Oct 09 '16 at 19:38
  • pls post converter code – Kenshin Oct 09 '16 at 19:47
  • Sorry for the delayed response. I added the converter. @Arun – JVz Oct 10 '16 at 19:28
  • I think the whole problem is "subcategoryController.categoryList". This is not the list you should be using. You need a list with categoryId objects and not subcat objects. Try to change something like this.... because in selectOneMenu, its cat obj, so selecteItems must also have cat object rather than subcat object – Kenshin Oct 11 '16 at 05:02
  • No yet, unfortunately. I don't know what I'm doing wrong. I'll keep investigating and, once done, I will post it here. Thanks, Man. @Arun – JVz Oct 12 '16 at 21:03
  • Problem solved. I was using the wrong entity in the selectOneMenu. I use: subcategoryController.category.categoryId, and the correct one, since I was working with subcategory entities is: subcategoryController.subcategory.categoryId – JVz Oct 31 '16 at 12:47

2 Answers2

1

After reviewing the code, I realized that I was using the wrong entity in the JSF.:

For example:

In the question, I posted: subcategoryController.category.categoryId to save Category object in a subcategory object.

As follow:

<p:selectOneMenu id="cboCategoryDialog" converter="subcategoryConverter"
    value="#{subcategoryController.category.categoryId}"

But, the correct approach is the following:

<p:selectOneMenu id="cboCategoryDialog" converter="omnifaces.SelectItemsConverter"
    value="#{subcategoryController.subcategory.categoryId}"

Now, this component is "pointing" to the correct object.

Another issue that I was facing is:

As I have nested entities, example: Entity.entity.entity they should be inicialized in every level, before using it.

Some posts that really, really, really helped me on this:

On solving the nested entities issues (PropertyNotFoundException).

Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable

On using normal and OmniFaces Converter on SelectOneMenu:

http://balusc.omnifaces.org/2007/09/objects-in-hselectonemenu.html

Community
  • 1
  • 1
JVz
  • 41
  • 2
  • 6
0

Be sure that your entity hashCode() and equals() methods are implemented and doesn't try to access nullable fields. That worked for me!