0

I am writing a simple JSF application that needs to implement CRUD operations in a database. Basically, it is a vacancy management application which allows candidates to apply to a job. To simplify the problem, suppose I have the following database:

    Users
------------
     Id
    Name
   Address
   CityId



   Cities
-------------
    Id
   City

I am using persistence and have two beans implemented: CityBean and UsersBean. The CityBean looks as following:

@ManagedBean
@SessionScoped
public class CityBean implements Serializable
{
   private static final long serialVersionUID = 1L;
   private List<City> cityList = new ArrayList<City>();
   public CityBean()
   {
        cityList.addAll(database.getInstance().getCities());
   }
  ///removed for simplicity
}

My UsersBean looks as following:

    @ManagedBean
    @SessionScoped
    public class UsersBean implements Serializable
    {
         private static final long serialVersionUID = 1L;
         public User user = new User();
         public String addUser(){
            database.getInstance().addUser(user);
            return "";
         }
    }

Finally, my xhtml page has the following structure:

 <h:form>
   <h:outputText value="${texts.name}" />
                <h:inputText id="inputName" value="#{usersBean.user.name}" styleClass="tb5" autocomplete="off">                          
                </h:inputText>

                <h:inputText id="inputSurname" value="#{usersBean.user.surname}" styleClass="tb5" autocomplete="off">           
                </h:inputText>

                <h:selectOneMenu value="#{cityBean.cityList}">
                    <f:selectItems value="#{cityBean.cityList}" var="c" itemValue="#{c.id}" itemLabel="#{c.name}"></f:selectItems>
                </h:selectOneMenu>
                <h:commandButton value="${texts.save}" styleClass="button" action="#{usersBean.addUser}">
            </h:commandButton>

   </h:form>

The xhtml page generates correctly. The dropdown is filled with items from the table Cities, and the value of each city is the id of the city. However, inserting fails without an error (nothing is inserted in the database).

On the other hand, if I comment the element (removing the dropdown from the view), a user is inserted in the database with NULL value for the CityId.

I am a novice programmer in JSF, this is my first project. I have a lot of experience in .Net and PHP and I would appreciate if you can point to me what am I doing wrong with this approach I take.

Edit (User entity):

@Entity
public class Wine implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private long id;
    private String name;
    private String surname;
    private String address;
    private int cityId;

    //Constructor left out (an empty and another with parameters)
    //Get/Set methods left out 

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user496607
  • 442
  • 1
  • 10
  • 21
  • Could you paste `User` entity bean source code in your question? `value` attribute is wrong in h:selectOneMenu component, but I need the entity source code for determining the answer. – Javier Haro Sep 12 '15 at 19:31
  • I added the user entity. – user496607 Sep 12 '15 at 20:47
  • What do you use for persisting your entities, JPA/Hibernate? – Javier Haro Sep 12 '15 at 20:56
  • i am using JPA for persistance. the database is derby. – user496607 Sep 12 '15 at 21:40
  • Add to get notified of any missing conversion/validation errors and pay a bit more love and attention to server logs; any missing conversion/validation errors are logged there. Each of them is quite googlable, e.g. http://stackoverflow.com/q/4734580 and http://stackoverflow.com/q/9069379. As to the failing action method invocation, see also http://stackoverflow.com/q/2118656. For future questions, please read http://stackoverflow.com/tags/jsf/info as well. All the DB fluff is irrelevant to the concrete problem and only adds noise to the question. – BalusC Sep 12 '15 at 22:53

2 Answers2

1

Create in your CityBean field to store chosen city/city.id and put in <h:selectOneMenu value="#{cityBean.cityList}"> instead of list. List is only supposed to be in selectItems.

Geinmachi
  • 1,251
  • 1
  • 8
  • 20
  • How should I implement this? Should I add private variables for city name and another for city id accompanied with get / set methods?! Can you please show me a sample code? – user496607 Sep 12 '15 at 20:49
  • Only private variable for city id since you have `itemValue="#{c.id}"` so when user chooses city from listmenu, after submit you will have city id from selected city. It's just this variable with getter/setter and using it in ``. You can see some example here, although it's PrimeFaces, the idea is the same - list in `` and chosen variable in `` (on the site there is `p:` instead of `h:` because of the framework). http://www.primefaces.org/showcase/ui/input/oneMenu.xhtml – Geinmachi Sep 12 '15 at 21:41
1

You have to replace this line:

<h:selectOneMenu value="#{cityBean.cityList}">

with this one:

<h:selectOneMenu value="#{usersBean.user.cityId}">

But you are not following Java EE best practices in your application. For example, you should have a OneToOne annotation for the User-City relation in the User entity . You should follow a good basic tutorial of JSF/JPA to get a good understanding of the basics. If you use Maven you can start from an JSF&JPA archetype.

Javier Haro
  • 1,255
  • 9
  • 14