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
}