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 6760825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:06:39+00:00 2026-05-26T14:06:39+00:00

I have this Entity called ‘Operation’: @Entity @Table(name=operation) public class Operation implements Serializable {

  • 0

I have this Entity called ‘Operation’:

@Entity
@Table(name="operation")
public class Operation implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private Integer id;

    @NotNull(message="informe um tipo de operação")
    private String operation;

    //bi-directional many-to-one association to Product
    @OneToMany(mappedBy="operation")
    private List<Product> products;

    // getter and setters
}

I retrieve the operations this way: (It could be through an EJB instance but just to keep it local and as an example, okay? 😉 )

public Map<String, Object> getOperations() {
    operations = new LinkedHashMap<String, Object>();
    operations.put("Select an operation", new Operation());
    operations.put("Donation", new Operation(new Integer(1), "donation"));
    operations.put("Exchange", new Operation(new Integer(2), "exchange"));

    return operations;
}

So I’m trying to get the selected operation in this selectOneMenu:

The productc is a ManagedBean which has a viewScope, productb is a ManagedBean with a sessionScope which has a product which is my entity. The product contais one operation, so is something like this:

(the letter c has the meaning of control, where all operations related about my entity product should be handled by this bean, okay?)

Product productc (ViewScope) 
-- ProductBean productb (SessionScope)
---- Product product (Entity)
-------- Operation operation (Entity)

The converter is the same as @BalusC is suggest before:

@ManagedBean
@RequestScoped
public class OperationConverter implements Converter {

    @EJB
    private EaoOperation operationService;

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

        return String.valueOf(((Operation) value).getId());
    }

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

        Operation operation = operationService.find(Integer.valueOf(value));
        System.out.println("Getting the operation value = " + operation.getOperation() );

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

        return operation;
    }

Which retrieve the operation selected as showed in the log:

FINE: SELECT ID, OPERATION FROM operation WHERE (ID = ?)
    bind => [1 parameter bound]
INFO: Getting the operation value = exchange

So when I try to submit the form gives the follow error:

form_add_product:operation: Validation error: the value is not valid

Why is this happening?

  • 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-26T14:06:39+00:00Added an answer on May 26, 2026 at 2:06 pm

    You’re trying to pass a complex object around as HTTP request parameter which can be only be a String. JSF/EL has builtin converters for primitives and its wrappers (e.g. int, Integer) and even enums. But for all other types you really need to write a custom converter. In this case you need to write a converter which converts between String and Operation. The String is then used as option value (open page in browser, rightclick and View Source and notice the <option value>). The Operation is then used as model value. The String should uniquely identify the Operation object. You could use operation ID for this.

    But in this particular case, with such a hardcoded map and a relatively simple model, I think it’s easier to use an enum instead.

    public enum Operation {
    
        DONATION("Donation"), EXCHANGE("Exchange");
    
        private String label;
    
        private Operation(String label) {
            this.label = label;
        }
    
        public string getLabel() {
            return label;
        }
    
    }
    

    with

    private Operation operation; // +getter +setter
    
    public Operation[] getOperations() {
        return Operation.values();
    }
    

    and

    <h:selectOneMenu value="#{bean.operation}">
        <f:selectItems value="#{bean.operations}" var="operation" itemValue="#{operation}" itemLabel="#{operation.label}" />
    </h:selectOneMenu>
    

    But if those values have actually to be retrieved from the DB and its size is undefinied, then you still really need a custom converter. You could in getAsString() return the ID and in getAsObject() use the operation DAO/EJB to get an Operation by the ID.

    @ManagedBean
    @RequestScoped
    public class OperationConverter implements Converter {
    
        @EJB
        private OperationService operationService;
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            // Convert here Operation object to String value for use in HTML.
            if (!(value instanceof Operation) || ((Operation) value).getId() == null) {
                return null;
            }
    
            return String.valueOf(((Operation) value).getId());
        }
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            // Convert here String submitted value to Operation object.
            if (value == null || !value.matches("\\d+")) {
                return null;
            }
    
            Operation operation = operationService.find(Long.valueOf(value));
    
            if (operation == null) {
                throw new ConverterException(new FacesMessage("Unknown operation ID: " + value));
            }
    
            return operation;
        }
    
    }
    

    Use it as follows:

    <h:selectOneMenu ... converter="#{operationConverter}">
    

    As to why it’s a @ManagedBean instead of @FacesConverter, read this: Converting and validating GET request parameters.


    Update as to the Validation Error: value not valid error, this means that the equals() method of the Operation class is broken or missing. During validation, JSF compares the submitted value with the list of available values by Object#equals(). If no one of the list matches with the submitted value, then you’ll see this error. So, ensure that equals() is properly implemented. Here’s a basic example which compares by the DB technical identity.

    public boolean equals(Object other) {
        return (other instanceof Operation) && (id != null) 
             ? id.equals(((Operation) other).id) 
             : (other == this);
    }
    

    Don’t forget to implement hashCode() as well:

    public int hashCode() {
        return (id != null) 
             ? (getClass().hashCode() + id.hashCode())
             : super.hashCode();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
I have a Person and an Organisation Entity: Person looks like this: public class
I have an entity called Account and I have given this entity a class
Imagine I have an entity called Product and a repository for it: public class
I have this entity class, called Pagina and I want to update the entry
I have entity called Customer and it has three properties: public class Customer {
i have this entity called BlogArticle which has a property called public virtual ICollection<BlogComment>
I have a entity called Product,this is a part of it's declration: [EdmEntityTypeAttribute(NamespaceName=NorthwindModel, Name=Product)]
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
I have a JPA object which has a many-to-many relationship like this: @Entity public

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.