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,